I have three tables 1-posts, 2-categories and 3-category_post, I've done all the necessary things, now the problem is here how can I get specific records of a category in controller for example I want to retrieve all those posts which their category is animal
thanks in advance
You will need to define their relationships:
class Posts extends Model {
public function category()
{
return $this->belongsToMany('App\Category');
}
}
class Category extends Model {
public function posts()
{
return $this->belongsToMany('App\Posts');
}
}
You also need a post_category pivot table, with two columns: post_id category_id
Post::with('category')->get(); // will return all posts and their categories