user9724533
user9724533

Reputation:

Use or statement in laravel

I have a messages table in my database and the query I want to perform is:

 $sql = 'SELECT * FROM MESSAGES WHERE sender_id = ' . $id. ' OR receiver_id = '.  $id;

That is what I am looking for. So, how do I do the very same thing in laravel? I searched in the laravel documentation and found something like SQL raw statements but I guess it can be vulnerable to SQL attacks, so how do I do it safely and sweetly?

Upvotes: 1

Views: 66

Answers (2)

Majbah Habib
Majbah Habib

Reputation: 8548

Your may use the following code to solve your problem.

   DB::table('messages')
                 ->where('sender_id', $id)
                 ->orWhere('reciever_id', $id)
                 ->get();

Upvotes: 0

Ryan Kozak
Ryan Kozak

Reputation: 1131

This is is the "Laravel Way" of building the raw query you've got.

DB::table('messages')->where('sender_id', '=', $id)
                     ->orWhere('reciever_id', '=', $id)->get();

Upvotes: 1

Related Questions