Sunday, April 9, 2017

Where clause inside whereHas being ignored in Eloquent

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

Im using Laravel 5.2 using a Postgres driver.

The Project model is:

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

The investments model has:

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

The projects table has fields id,fields...

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

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?



via Jonathan

Advertisement