嗨,我在拉拉维尔工作。
我需要用选定的company_id从数据库中选择所有行。在表单中,company_id是多选择的
这就是我所做的
$data = $request->All();
$notifyTo = $data['notify_to']; /**value from multiselect which is an array**/
foreach($notifyTo as $noTo => $value){
$user = User::all()->where('company_id', "=", $value)->where('status', "=", 1);
}
print_r($user); exit;
这里,$data‘Array ( [0] => 11 [1] => 22 [2] => 33 )
_to’是来自multiselct的值,它是company_id
Array ( [0] => 11 [1] => 22 [2] => 33 )
的数组
表结构
| id | name | company_id | status |
-----------------------------------
发布于 2022-09-16 06:10:00
您可以使用
User::whereIn('company_id', $data['notify_to'])->where('status', 1)->get();
它会解决你的问题。
如果您有多个筛选器,并且希望添加多个"if条件“(如果筛选器设置正确),则使用以下代码结构。
$query = User::where('status', 1);
if (isset(filters['filtername']) {
$query->where(filters['filtername']);
}
multiple filter conditions.
...
...
$userObject = $query->get;
它将提供一个基于应用过滤器的用户集合。
发布于 2022-09-16 07:45:59
使用whereIn()
子句
$data = $request->All();
$notifyTo = $data['notify_to'];
$user = User::whereIn('company_id', $notifyTo)->whereStatus(1)->get();
print_r($user);
确保
$notifyTo
是一个索引数组。如果不使用array_values()
https://stackoverflow.com/questions/73735537
复制相似问题