如何在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();
答案 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
进一步链接