Eloquent orWhere() 无法按预期与之前的 where() 方法一起使用
Eloquent orWhere() is not working with previous where() methods as expected
我正在开发好友请求路由。
希望我的代码能够正常工作
$friends=[];
foreach (auth()->user()->friends as $friend)
$friends[]=$friend->id;
$people = User::query()
->where('id', '!=', auth()->user()->id)
->whereNotIn('id', $friends)
->where('name', 'LIKE', "%$request->search%")
->orWhere('email', 'LIKE', "%$request->search%")
->get();
但是这段代码的工作方式是
where ( 'id'!=auth()->user()->id and 'id' not in [...] and 'name' like '$name' ) or 'email' like '$email'
我希望它能像
where 'id'!=auth()->user()->id and 'id' not in [...] and ('name' like '$name' or 'email' like '$email')
然后我将代码更改为:
$friends=[];
foreach (auth()->user()->friends as $friend)
$friends[]=$friend->id;
$people = User::query()
->where('name', 'LIKE', "%$request->search%")
->where('id', '!=', auth()->user()->id)
->whereNotIn('id', $friends)
->orWhere('email', 'LIKE', "%$request->search%")
->where('id', '!=', auth()->user()->id)
->whereNotIn('id', $friends)
->get();
这对我有用,但我认为它不合适。
请指导。
您需要为此使用查询。
是这样的:
更多信息,你可以查看这个Documentation
$people = User::query()
->where('id', '!=', auth()->user()->id)
->whereNotIn('id', $friends)
->where(function ($query) {
$query->where('name', 'LIKE', "%$request->search%")
->orWhere('email', 'LIKE', "%$request->search%")
})->get();
您可以像这样使用“参数分组”:
$people = User::query()
->where('id', '!=', auth()->user()->id)
->whereNotIn('id', $friends)
->where(function ($query) use ($request) {
$query->where('name', 'LIKE', "%$request->search%")
->orWhere('email', 'LIKE', "%$request->search%");
})
->get();
As you can see, passing a Closure
into the where
method instructs the query builder to begin a constraint group. The Closure
will receive a query builder instance which you can use to set the constraints that should be contained within the parenthesis group.
有关详细信息,请参阅 Laravel docs。
我正在开发好友请求路由。
希望我的代码能够正常工作
$friends=[];
foreach (auth()->user()->friends as $friend)
$friends[]=$friend->id;
$people = User::query()
->where('id', '!=', auth()->user()->id)
->whereNotIn('id', $friends)
->where('name', 'LIKE', "%$request->search%")
->orWhere('email', 'LIKE', "%$request->search%")
->get();
但是这段代码的工作方式是
where ( 'id'!=auth()->user()->id and 'id' not in [...] and 'name' like '$name' ) or 'email' like '$email'
我希望它能像
where 'id'!=auth()->user()->id and 'id' not in [...] and ('name' like '$name' or 'email' like '$email')
然后我将代码更改为:
$friends=[];
foreach (auth()->user()->friends as $friend)
$friends[]=$friend->id;
$people = User::query()
->where('name', 'LIKE', "%$request->search%")
->where('id', '!=', auth()->user()->id)
->whereNotIn('id', $friends)
->orWhere('email', 'LIKE', "%$request->search%")
->where('id', '!=', auth()->user()->id)
->whereNotIn('id', $friends)
->get();
这对我有用,但我认为它不合适。 请指导。
您需要为此使用查询。
是这样的:
更多信息,你可以查看这个Documentation
$people = User::query()
->where('id', '!=', auth()->user()->id)
->whereNotIn('id', $friends)
->where(function ($query) {
$query->where('name', 'LIKE', "%$request->search%")
->orWhere('email', 'LIKE', "%$request->search%")
})->get();
您可以像这样使用“参数分组”:
$people = User::query()
->where('id', '!=', auth()->user()->id)
->whereNotIn('id', $friends)
->where(function ($query) use ($request) {
$query->where('name', 'LIKE', "%$request->search%")
->orWhere('email', 'LIKE', "%$request->search%");
})
->get();
As you can see, passing a
Closure
into thewhere
method instructs the query builder to begin a constraint group. TheClosure
will receive a query builder instance which you can use to set the constraints that should be contained within the parenthesis group.
有关详细信息,请参阅 Laravel docs。