雄辩中被忽略的Where子句 [英] Where clause inside whereHas being ignored in Eloquent

查看:121
本文介绍了雄辩中被忽略的Where子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 whereHas 进行查询。查询是这样的:

Im trying to make a query using whereHas with eloquent. The query is like this:

$projects = Project::whereHas('investments', function($q) {
                $q->where('status','=','paid');
            })
            ->with('investments')
            ->get();

我使用Postgres驱动程序使用Laravel 5.2。

Im using Laravel 5.2 using a Postgres driver.

项目模型是:

public function investments()
{
    return $this->hasMany('App\Investment');
}

投资模式有:

public function project() {
    return $this->belongsTo('App\Project');
}

项目表中有字段id,字段...

The projects table has fields id,fields...

投资表有字段id,project_id,status,created_at

The investments table has the fields id,project_id,status,created_at

我的问题是查询运行并返回一个至少有一个投资的项目,但是忽略 where 之内的 条款,因为结果集合包括状态值不同于支付的投资 EM>。

My issue is that the query runs and returns a collection of the projects which have at least one investment, however the where clause inside the whereHas is ignored, because the resulting collection includes investments with status values different than paid.

有没有人知道发生了什么?

Does anyone has any idea of what is going on?

推荐答案

更改订单。使用with()之前的whereHas()。几个星期前我有类似的问题。 Btw是问题与您所做的功能实例之间唯一的真正区别。

Change the order. Use with() before the whereHas(). I had a similar problem few weeks ago. Btw, is the only real difference between the problem and the functional example that you made.

这篇关于雄辩中被忽略的Where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆