((何处和何处)或(何处和何处))Laravel 5.2

时间:2016-09-07 13:31:05

标签: php eloquent laravel-5.2 where-clause

我正在尝试创建((Where and Where)OR(Where and Where))经过大量搜索后我发现了这个

@media (max-width: 850px){    
    nav#site-navigation.fixed {
         position: initial !important;
    }    
}

但它告诉我 $ sender $ receiver 变量未定义,请帮助,我正在尝试显示两个用户的消息,我还想我可以单独获得第一个(在哪里和哪里)然后获得第二个然后我将合并它们。

2 个答案:

答案 0 :(得分:8)

要在$send中使用$receiverClosure,请执行以下操作;

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

}

允许您访问该函数范围内的变量。

所以你的解决方案就变成了;

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

答案 1 :(得分:3)

您需要使用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();