如何正确分组 Laravel Query Builder 中的 where 子句
How to group where clauses in Laravel Query Builder correctly
我 运行 使用下面的 search() 函数进行以下查询 - 问题是我需要对 where 子句进行分组 - 我做错了什么?
select `standings`.*, `users`.`name` as `user` from `standings`
left join `users` on `standings`.`user_id` = `users`.`id`
where `users`.`name` like '%bob%' or `users`.`email` like '%bob%'
and `standings`.`tenant_id` = '1'
在我的 Standings 模型中,我有以下执行 WHERE
子句的 search()
public static function search($query)
{
return empty($query) ? static::query()
: static::where('users.name', 'like', '%'.$query.'%')
->orWhere('users.email', 'like', '%'.$query.'%');
}
public function render()
{
$query = Standing::search($this->search)
->select('standings.*', 'users.name AS user')
->leftJoin('users', 'standings.user_id', '=', 'users.id')
->orderBy('points', 'desc')
->orderBy('goals_difference', 'desc')
->orderBy('goals_for', 'desc');
if($this->super && $this->selectedTenant) {
$query->where('standings.tenant_id', $this->selectedTenant);
}
return view('livewire.show-standings', [
'standings' => $query->paginate($this->perPage)
]);
}
查询有效,但它没有在 users.name 和 users.email 字段上正确分组 WHERE 子句 - 我如何更改此 search() 函数以便 WHERE 查询将它们分组为这个
where (`users`.`name` like '%bob%' or `users`.`email` like '%bob%')`
您需要将 where 子句分组到一个 wrapping where 子句中。试试这个
public static function search($query)
{
return empty($query)
? static::query()
: static::where(function($query){
$query->where('users.name', 'like', '%'.$query.'%')
->orWhere('users.email', 'like', '%'.$query.'%');
});
}
Thanks that for some reason even though looks correct gives me the following error - Object of class Illuminate\Database\Eloquent\Builder could not be converted to string NB I am using Laravel with Livewire (not sure if that should make any difference)
$query->where('users.name', 'like', '%'.$query.'%')
和 ->orWhere('users.email', 'like', '%'.$query.'%');
给出了错误,因为在尝试比较 $query 时被视为字符串,因此错误
您可以将搜索定义为模型上的查询范围
//Assuming a relation Standing belongsTo User
//Query constraint to get all Standing records where
//related User record's name or email are like searchTerm
public function scopeSearch($query, string $searchTerm)
{
return $query->whereHas('user', function($query) use($searchTerm){
$query->where('name', 'like', "%{$searchTerm)%")
->orWhere('email', 'like', "%{$searchTerm}%");
});
}
Laravel 文档:https://laravel.com/docs/8.x/eloquent#local-scopes
通过在 Standing 模型上定义上述搜索范围,您可以将渲染功能设置为
public function render()
{
$query = Standing::with('user:id,name')
->search($this->search)
->orderBy('points', 'desc')
->orderBy('goals_difference', 'desc')
->orderBy('goals_for', 'desc');
if($this->super && $this->selectedTenant) {
$query->where('tenant_id', $this->selectedTenant);
}
return view('livewire.show-standings', [
'standings' => $query->paginate($this->perPage)
]);
}
我 运行 使用下面的 search() 函数进行以下查询 - 问题是我需要对 where 子句进行分组 - 我做错了什么?
select `standings`.*, `users`.`name` as `user` from `standings`
left join `users` on `standings`.`user_id` = `users`.`id`
where `users`.`name` like '%bob%' or `users`.`email` like '%bob%'
and `standings`.`tenant_id` = '1'
在我的 Standings 模型中,我有以下执行 WHERE
子句的 search()
public static function search($query)
{
return empty($query) ? static::query()
: static::where('users.name', 'like', '%'.$query.'%')
->orWhere('users.email', 'like', '%'.$query.'%');
}
public function render()
{
$query = Standing::search($this->search)
->select('standings.*', 'users.name AS user')
->leftJoin('users', 'standings.user_id', '=', 'users.id')
->orderBy('points', 'desc')
->orderBy('goals_difference', 'desc')
->orderBy('goals_for', 'desc');
if($this->super && $this->selectedTenant) {
$query->where('standings.tenant_id', $this->selectedTenant);
}
return view('livewire.show-standings', [
'standings' => $query->paginate($this->perPage)
]);
}
查询有效,但它没有在 users.name 和 users.email 字段上正确分组 WHERE 子句 - 我如何更改此 search() 函数以便 WHERE 查询将它们分组为这个
where (`users`.`name` like '%bob%' or `users`.`email` like '%bob%')`
您需要将 where 子句分组到一个 wrapping where 子句中。试试这个
public static function search($query)
{
return empty($query)
? static::query()
: static::where(function($query){
$query->where('users.name', 'like', '%'.$query.'%')
->orWhere('users.email', 'like', '%'.$query.'%');
});
}
Thanks that for some reason even though looks correct gives me the following error - Object of class Illuminate\Database\Eloquent\Builder could not be converted to string NB I am using Laravel with Livewire (not sure if that should make any difference)
$query->where('users.name', 'like', '%'.$query.'%')
和 ->orWhere('users.email', 'like', '%'.$query.'%');
给出了错误,因为在尝试比较 $query 时被视为字符串,因此错误
您可以将搜索定义为模型上的查询范围
//Assuming a relation Standing belongsTo User
//Query constraint to get all Standing records where
//related User record's name or email are like searchTerm
public function scopeSearch($query, string $searchTerm)
{
return $query->whereHas('user', function($query) use($searchTerm){
$query->where('name', 'like', "%{$searchTerm)%")
->orWhere('email', 'like', "%{$searchTerm}%");
});
}
Laravel 文档:https://laravel.com/docs/8.x/eloquent#local-scopes
通过在 Standing 模型上定义上述搜索范围,您可以将渲染功能设置为
public function render()
{
$query = Standing::with('user:id,name')
->search($this->search)
->orderBy('points', 'desc')
->orderBy('goals_difference', 'desc')
->orderBy('goals_for', 'desc');
if($this->super && $this->selectedTenant) {
$query->where('tenant_id', $this->selectedTenant);
}
return view('livewire.show-standings', [
'standings' => $query->paginate($this->perPage)
]);
}