如何在laravel 5.2中编写条件查询?

时间:2016-10-11 11:12:15

标签: php laravel-5.2

如何在laravel 5.2 eloquent或query builder中编写此类查询?

$query = Customers::leftjoin('Query string');    
if(condition)
    {
        //This part added to query   
    }
    else
    {
        //This part added to query
    }
->get();

1 个答案:

答案 0 :(得分:3)

您必须在变量I.E中记住您的查询。像这样:

$query = Customers::leftjoin('Query string');   

if(condation){
    $query = $query->where('something', '=', 'something');
} else {
    $query = $query->where('somethingelse', '=', 'somethingelse');
}

$query = $query->get();

通常你会$query->where()-get();,这基本上是相同的,$query将保留->where()的结果,并允许你在$query进一步链接