Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用Laravel 5.2中的orWhere查询范围_Php_Laravel_Laravel 5_Eloquent_Laravel 5.2 - Fatal编程技术网

Php 使用Laravel 5.2中的orWhere查询范围

Php 使用Laravel 5.2中的orWhere查询范围,php,laravel,laravel-5,eloquent,laravel-5.2,Php,Laravel,Laravel 5,Eloquent,Laravel 5.2,使用Laravel 5.2,我的模型具有以下查询范围: public function scopeInProcess($query) { return $query->whereHas( 'ApplicationStatus', function($query) { $query->whereRaw('(name = "New" OR name = "In-process")'); } ); } 上面的操作很好

使用Laravel 5.2,我的模型具有以下查询范围:

public function scopeInProcess($query) {
    return $query->whereHas(
        'ApplicationStatus', function($query) {
            $query->whereRaw('(name = "New" OR name = "In-process")');
        }
    );
}
上面的操作很好,但我只是开始使用
whereRaw()
,因为我无法让
或where()
按照文档中的描述工作

据我所知,这应该与
whereRaw()
完全相同:


不过,它不起作用。它只返回所有记录,包括具有其他状态名称的记录。

它没有执行完全相同的操作。在
whereRaw()
中,将这两个条件都用括号括起来,以便将它们组合在一起。这就是你想要的。但是,
where()->或where()
不会自动执行此操作

要获得所需的功能,需要对条件进行分组,就像在
whereRaw()
中所做的那样。可以通过向
where()
方法传递闭包来完成此操作,如下所示:

public function scopeInProcess($query) {
    return $query->whereHas('ApplicationStatus', function($query) {
        $query->where(function($q) {
            $q->where('name', 'New')->orWhere('name', 'In-process');
        });
    });
}

您是否在“$query->where(…);”之前添加了“return”?感谢您提供的解决方案,感谢您如此清晰地解释它@帕特里克斯-你能试着回答这个问题吗-?
public function scopeInProcess($query) {
    return $query->whereHas('ApplicationStatus', function($query) {
        $query->where(function($q) {
            $q->where('name', 'New')->orWhere('name', 'In-process');
        });
    });
}