Reputation: 157
I want to get POSTS from the CATEGORIE i do the relationship HasMany/belongsTo but it gives me error.
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]);
}
Upvotes: 1
Views: 55
Reputation: 2683
First of all you called model Categorie
but using Category
.
return $this->belongsTo('App\Category');
->
return $this->belongsTo('App\Categorie');
This might be not the whole solution.
After that check the name of foreign key column in posts table. And change/add it in database or pass as the second parameter to belongsTo()
relation.
Upvotes: 1
Reputation: 783
The mistake is in your SiteController
public function getPostsOfCategory($slug)
{
$categorie=Categorie::where('slug',$slug)->first();
$posts= $categorie->posts;
$categories=Categorie::all();
return view('site.blog',['posts'=>$posts,'categories'=>$categories]);
}
Upvotes: 0