laravel – OrderBy工作但不在哪里

这很好用.

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

这不是:

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

还尝试了wherePivot中的关系:

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

最佳答案 尝试首先在关系中添加
withPivot()

->withPivot('approved');

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

点赞