【问题标题】:How do I combine multiple and or conditions in octoberCMS query?如何在 octoberCMS 查询中组合多个 and or 条件?
【发布时间】:2023-03-27 10:25:02
【问题描述】:

我正在定制一个基于 octoberCMS(基于 laravel)的预建项目。 Bellow 是一个现有的查询 sn-ps,我想在其中添加更多的“AND”和“OR”条件。

注释掉是我需要做的简单的话或接近 程序风格

$records  = DB::orderBy('id', 'desc');
$records  = $records->where(function($q){
                        $q->where(function($q){
                            //Get all employees
                            $q->where('assigned_id', Auth::getUser()->id);
                        });
//I need to add "OR (' "column_x" = "whatever" AND "column_y" = "whatever" ')"
                        $q->orWhere(function($q){
                            $q->whereHas('manifest', function($q){
                                $q->where(function($q) {
                                    $q->where('driver_id', Auth::getUser()->id);
                                });
                                $q->orWhere(function($q){
                                    $q->where('employee_id', Auth::getUser()->id);
                                });
                            });
                        });
                    });

【问题讨论】:

    标签: mysql octobercms


    【解决方案1】:

    最好的方法是通过模型提出请求。不要使用生成器

      $iUserId =  Auth::getUser()->id;
    
    $records  = MyModel::where('assigned_id', $iUserId)
        ->orWhere(function($q) use ($iUserId) {
            $q->whereHas('manifest', function($q) use ($iUserId){
                $q->where('driver_id', $iUserId);
                $q->orWhere('employee_id', $iUserId);
            });
        });
    
    $records->orderByDesc('id')->get();
    

    【讨论】:

      猜你喜欢
      • 2017-09-09
      • 2012-06-18
      • 2012-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-10
      相关资源
      最近更新 更多