过滤嵌套的预先加载

时间:2018-01-08 21:09:48

标签: php laravel eloquent

我有下一个问题/问题:

我正在尝试加入那些表格并过滤其中一个。

内容型号:

public function credits(){
     return $this->belongsToMany('App\models\Credit');
}

学分型号:

public function person(){
    return $this->belongsTo('App\models\Person')->withDefault();
}

型号:

人物模型具有名为受欢迎程度的属性(对于该示例很重要)

public function image(){
    return $this->hasOne('App\models\Images_persons')->where('is_backdrop',0)->orderBy('vote_count','desc');
}

示例:

当我查询一部电影时,我获得了很多学分,所以我们假设 kill_bill 电影有40个学分

Kill_bill 40个学分,每个学分只有一个人

我要做的只是一部电影需要5个学分,按人口人口过滤。

结论:我搜索一部电影,我得到了电影的学分与该学分的人加入,我按照该人的受欢迎程度排序然后我拿前5个学分,显示还有人的信息。

尝试代码:

$content_movie=$this->content::with([

      'credits'=>function($query){
            $query->limit('person',function($query2){
                 $query2->orderBy('popularity','desc')->take(5);
            })->with('person');
       },

 ])->where('slug',$args['slug'])->get(['id','name','collections_id','overview','release_date','status','video','vote_average']);

除了这段代码,我已经测试了很多代码,但是我还没有获得任何结果

0 个答案:

没有答案
相关问题