【发布时间】:2021-03-17 23:22:30
【问题描述】:
我正在使用下面的 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'
在我的排名模型中,我有以下 search() 执行 WHERE 子句
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%')`
【问题讨论】:
-
你在寻找类似this的东西
标签: laravel eloquent laravel-7 laravel-query-builder laravel-livewire