Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

( (Where and Where) OR (Where and Where) ) Laravel 5.2

I am trying to create ( (Where and Where) OR (Where and Where) ) And after a lot of searching I found this

    $sender = \App\User::where('username','=',$username)->firstOrFail();
    $receiver = Auth::user();
    $messages = \App\Message::Where(function($query)
                              {
                                  $query->where("sender",$sender->id)
                                        ->where("receiver",$receiver->id);
                              })
                              ->orWhere(function($query)
                              {
                                  $query->Where("sender",$receiver->id)
                                        ->Where("receiver",$sender->id);
                              })
                              ->get();

But it's showing me that the $sender and the $receiver variables are undefined , Any help please , I'm trying to show the messages of the both users, I thought also that I can get the first (where and where) alone and then get the second one then I'll merge them.

like image 769
Khalil Bz Avatar asked Sep 07 '16 13:09

Khalil Bz


People also ask

What is difference between where and whereIn in Laravel?

Note: where will compare with just first value of array or just one single value. and whereIn will compare evey index of array.

Where can I find Laravel?

Example to Implement Laravel Find As we know, the find () method in Laravel can be used by any user along with an array of primary keys, and it will return a set of matching records from the database. For example, $student = Students::all (); With the help of the above data, we can get the details of all the students.

Where are Laravel models?

Models in Laravel 5.5 are created inside the app folder. Models are mostly used to interact with the database using Eloquent ORM.

Where do I put Laravel code?

Blade is the Laravel template, you should not put your code inside the blade file, it is not a good practice (not recommended), just things you are going to show on the final page. You should put your code inside the controller.


2 Answers

To use $send and $receiver within a Closure do the following;

function ($query) use($sender, $receiver){

}

Allowing you to access the variables within the scope of that function.

So your solution becomes;

$messages = \App\Message::Where(function($query) use ($sender, $receiver)
                          {
                              $query->where("sender",$sender->id)
                                    ->where("receiver",$receiver->id);
                          })
                          ->orWhere(function($query) use ($sender, $receiver)
                          {
                              $query->Where("sender",$receiver->id)
                                    ->Where("receiver",$sender->id);
                          })
                          ->get();
like image 53
Matt Burrow Avatar answered Oct 16 '22 19:10

Matt Burrow


You need to pass the variables through with use:

$messages = \App\Message::Where(function($query) use ($sender, $receiver)
                          {
                              $query->where("sender",$sender->id)
                                    ->where("receiver",$receiver->id);
                          })
                          ->orWhere(function($query) use ($sender, $receiver)
                          {
                              $query->Where("sender",$receiver->id)
                                    ->Where("receiver",$sender->id);
                          })
                          ->get();
like image 45
aynber Avatar answered Oct 16 '22 19:10

aynber