【问题标题】:how to use orWhere() with where in laravel如何在 laravel 中使用 orWhere() 和 where
【发布时间】:2021-10-10 08:53:30
【问题描述】:

我试图在 where 和 whereIn 之后使用 orWhere 但如果 orWhere 为 true 它会返回 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)

这些是或其中任何一个都可能为真的地方

->orWhereIn('ranch',$staff_ids)->orWhereIn('cutting_company',$staff_ids)

【问题讨论】:

    标签: laravel eloquent laravel-query-builder


    【解决方案1】:

    试试这个:

    $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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-12
      • 2020-01-01
      • 2016-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-19
      • 2021-02-11
      相关资源
      最近更新 更多