我想在查询构建器的末尾添加一个"AND"子句,代码如下所示:
$orderers = DB::table('address')->where(function($query) use ($term) {
$query->where('id', 'LIKE', '%' . $term . '%')
->or_where('name', 'LIKE', '%' . $term . '%')
->or_where('status', 'LIKE', '%' . $term . '%')
->and_clause_goes_here('is_orderer', '=', '1');
})->paginate($per_page);
Run Code Online (Sandbox Code Playgroud)
但是在Laravel中搜索AND条款我找不到任何等价物.你能帮我解决这个问题吗?
Col*_*mes 25
由于操作顺序,JCS解决方案仍可能产生一些意外结果.您应该像在SQL中一样将所有OR组合在一起,明确定义逻辑.它还使您在下次(或其他团队成员)阅读代码时更容易理解.
SELECT * FROM foo WHERE a = 'a'
AND (
WHERE b = 'b'
OR WHERE c = 'c'
)
AND WHERE d = 'd'
Foo::where( 'a', '=', 'a' )
->where( function ( $query )
{
$query->where( 'b', '=', 'b' )
->or_where( 'c', '=', 'c' );
})
->where( 'd', '=', 'd' )
->get();
Run Code Online (Sandbox Code Playgroud)
小智 14
只是另一个where子句会做,AND将被使用
$orderers = DB::table('address')->where(function($query) use ($term) {
$query->where('id', 'LIKE', '%' . $term . '%')
->where('is_orderer', '=', '1');
->or_where('name', 'LIKE', '%' . $term . '%')
->or_where('status', 'LIKE', '%' . $term . '%')
})->paginate($per_page);
Run Code Online (Sandbox Code Playgroud)