【发布时间】:2019-12-01 06:52:07
【问题描述】:
我想从 CATEGORIE 中获取 POSTS,我建立了 HasMany/belongsTo 关系,但它给了我错误。
Categorie.php
public function posts(){
return $this->hasMany('App\Post');
}
Post.php
public function category(){
return $this->belongsTo('App\Category');
}
SiteController.php
public function getPostsOfCategory($slug){
$categorie=Categorie::where('slug',$slug)->first();
$posts= $categorie->posts()->paginate(4);
$categories=Categorie::all();
return view('site.blog',['posts'=>$posts,'categories'=>$categories]);
}
【问题讨论】: