如何在 Laravel 中使用 orWhere() 和 where

斯威夫特解决方案

我试图在 where 和 whereIn 之后使用 orWhere 但如果 orWhere 为真,则返回 data 。它应该首先根据这些条件检查 where 和 whereIn 数据存在,然后才应该检查 orWhere 。

这是查询

   $staff_ids = Staff::select('id')->where('name','like',"%{$request->keyword}%")->pluck('id');
       $work_order_ids = WorkSheet::select('work_order_id')->where('worker_data','like',"%{$worker_id}%")->pluck('work_order_id');
       $work_orders =  WorkingOrders::whereIn('status',[8,9])->whereIn('id',$work_order_ids)->where('id',$request->keyword)->orWhereIn('ranch',$staff_ids)->orWhereIn('cutting_company',$staff_ids)->get();

有必须检查的条件

whereIn('status',[8,9])->whereIn('id',$work_order_ids)->where('id',$request->keyword)

这些是 orWhere 其中任何一个都可以是真的

->orWhereIn('ranch',$staff_ids)->orWhereIn('cutting_company',$staff_ids)
亚辛帕特尔

尝试这个:

$work_orders =  WorkingOrders::whereIn('status',[8,9])
->whereIn('id',$work_order_ids)
->where('id',$request->keyword)
->where(function($q) use($staff_ids){
  $q->whereIn('ranch',$staff_ids)
    ->orWhereIn('cutting_company',$staff_ids);
})
->get();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章