I have just got started with laravel v3 and am trying to wrap my head around eloquent's One-To-Many relationships by creating a blog, I have posts that have a many to one relationship with categories (Each Post is linked to a category).
I have the following tables with the following fields:
posts: id, title, body, date_created, category_id
categories: id, name
I have the following two models:
class Category extends Eloquent
{
public function posts()
{
return $this->has_many('Post');
}
}
class Post extends Eloquent
{
public function categories()
{
return $this->belongs_to('Category');
}
}
I figured out how to get all posts by passing in a category id:
category::find(2)->posts()->get())
I just need help on finding out how to get all posts, and get their corrisponding categories. So at the end of the day in the view I can output something like this:
{$post->title} - Category: {$post->category->name}
Thanks for any help!