如何在octoberCMS查询中组合多个和/或条件?

问题描述

我正在自定义一个基于 octoberCMS(基于 laravel)的预构建项目。 Bellow 是一个现有的查询片段,我想在其中添加更多“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);
                                });
                            });
                        });
                    });

解决方法

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

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...