我想像这样做一些查询
$query = User::where($a,$b);
if($options){
$query = $query->where($c,$d)->get();
}
else{
$query->get();
}
但是$query
有问题,那么如何在查询期间实现选择?
答案 0 :(得分:0)
你可以这样做:
$users = User::query();
if($options) {
$users->where(a$, $b);
} else {
$users->where($c, $d);
}
$users->get();
答案 1 :(得分:0)
我认为你所寻找的是这样的:
$query = User::where($a, $b);
if ($options) {
$query->where($c, $d);
}
if ($c) {
$query->orderBy('id', DESC);
}
// You can add other options to your query before calling the get() function
$query = $query->get();
如果$ options为true,那么它将运行此SQL:
select * from users where $a = $b and $c = $d
如果$ options为false:
select * from users where $a = $b