OrderBy works, but not there

This works great.

$q=Question::with(['users'=>function($query)
    {
        $query->orderBy('pivot_approved','desc');
    }])->get();

It does not:

 $q=Question::with(['users'=>function($query)
        {
            $query->where('pivot_approved',1);
        }])->get();

Also tried with wherePivot regarding:

public function users()
{     
    return $this->belongsToMany('App\User','question_user','q_id','user_id')->wherePivot('approved',1);  
}
+4
source share
1 answer

Try adding withPivot()to the relation:

->withPivot('approved');

By default, only model keys will be present on the summary object. If your pivot table contains additional attributes, you must specify them when defining the relationship.

+2
source

Source: https://habr.com/ru/post/1658578/


All Articles