php - ( (Where and Where) OR (Where and Where) ) Laravel 5.2 -


i trying create ( (where , where) or (where , where) ) , after lot of searching found

    $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 $sender , $receiver variables undefined , please , i'm trying show messages of both users, thought can first (where , where) alone , second 1 i'll merge them.

to use $send , $receiver within closure following;

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

allowing access variables within scope of function.

so 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(); 

Comments

Popular posts from this blog

serialization - Convert Any type in scala to Array[Byte] and back -

matplotlib support failed in PyCharm on OSX -

python - Matplotlib: TypeError: 'AxesSubplot' object is not callable -