我想编写一个集合,其中放置了多个where子句。但是根据某些条件,我想执行一些where子句并跳过其中的一些子句。预先感谢。
答案 0 :(得分:0)
您可以使用when()方法。 https://laravel.com/docs/5.7/queries#conditional-clauses 例如:
DB::table('products')
->when($request->active, function($query) {
$query->where('active','=',true);
})
->when(isset($request->category), function($query) use ($request) {
$query->where('category','=', $request->category);
})
->get();