【发布时间】:2019-04-22 23:37:26
【问题描述】:
我想要多对多的关系。所以我创建了: 进入游戏模型
public function category(){
return $this->belongsToMany('App\Category');
}
进入类别模型
public function games(){
return $this->hasMany('App\Game');
}
进入控制器
$category = Category::where('slug', $slug)->first();
dd($category->games());
return view('frontend.game.gamelist')->with('elements', $category->games());
一般来说,我会尝试显示所有属于特定类别的游戏。我看到这样的东西
如果我删除 dd() 视图将不会显示任何元素。但视图没有问题。
@foreach($elements as $element)
//...
@endforeach
为什么它不起作用?
【问题讨论】:
-
必须是
$category->games而不是$category->games(),像属性一样调用。
标签: laravel many-to-many relationship