我有自定义indexQuery
$resourceTable = 'soft';
$user = Auth::user();
$orderBy = $request->get('orderBy');
$dir = $request->get('orderByDirection');
$query->select(DB::raw("{$resourceTable}.*"));
if (!$user->isGlobalAdmin) {
$roles = json_decode($user->getRoleNames());
$rolesArray = [];
foreach ($roles as $roleName) {
$role = json_decode(Role::findByName($roleName));
$rolesArray[] = $role->id;
}
$query->join('model_has_roles', 'model_id', '=', "{$resourceTable}.id")
->where('model_type', '=', 'App\Models\Soft')
->whereIn('role_id', $rolesArray);
}
当我加载索引页的资源sql是
select soft.* from `soft`
inner join `model_has_roles` on `model_id` = `soft`.`id`
where `model_type` = 'App\\Models\\Soft' and `role_id` in (2, 3)
我有过滤器与角色。
应用代码为
$roles = $value;
$rolesArray = [];
foreach ($roles as $roleName) {
$role = json_decode(Role::findByName($roleName));
$rolesArray[] = $role->id;
}
return $query->whereIn('role_id', $rolesArray);
当我在filter中设置一个时,我得到sql
select soft.* from `soft`
inner join `model_has_roles` on `model_id` = `soft`.`id`
where `role_id` in (3) and `model_type` = App\\Models\\Soft and `role_id` in (2, 3)
2次role_id IN,如何将其更正为仅一次?
1条答案
按热度按时间wgxvkvu91#
通过确保每个role_id在查询时都在第一个或第二个数组
$rolesArray
中:$query->whereIn('role_id', $rolesArray);
并删除两个查询语句中的一个