Laravel hasManyThrough关系给出空集合

时间:2015-10-22 17:35:27

标签: mysql laravel laravel-5 eloquent relationship

我试图让所有过滤器都属于custom_view。例如:$user->custom_views->first->filters

下面的代码给了我一个空集合。我已经尝试了所有的东西,但无法解决为什么我得到一个空的集合。也许有人可以看到我在这里做错了什么?

user      custom_views   custom_view_filters   filters
----      ------------   -------------------   -------
id        id             id                    id
          user_id        custom_view_id
                         filter_id

用户模型:

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

CustomView模型:

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

public function filters(){
    return $this->hasManyThrough('App\Filter', 'App\CustomViewFilter', 'custom_view_id', 'id');
}

CustomViewFilter模型:

no relations defined

过滤模型

public function custom_views(){
    return $this->belongsToMany('App\CustomView', 'custom_view_filter', 'custom_view_id', 'filter_id');
}

1 个答案:

答案 0 :(得分:0)

filters中的CustomView方法更改为:

public function filters(){
    return $this->belongsToMany('App\Filter', 'custom_view_filter','filter_id','custom_view_id');
}