Actually, the relation between Post
and Category
could be many-to-many
because a Category
can has multiple posts under it and also a Post
could be in more than one Category
. So, if this is the case then you should create three tables like:
Table - posts
:
id | post_title | post_slug | post_content | Others...
Table - categories
:
id | category_title | category_slug | Others...
Table - category_post
(Pivot table/ maintains relation between posts and categories):
id | category_id | post_id
Then you need two
Models as Post
and Category
:
// Post model
class Post extends Eloquent {
public function categories() {
return $this->belongsToMany('Category');
}
}
// Category model
class Category extends Eloquent {
public function posts() {
return $this->belongsToMany('Post');
}
}
Create categories using something like this:
category::create(array(...)); // Input::all() (Mass Assignment)
Also you may create a Category
using:
$category = new category;
$category->category_title = Input::get('category_title');
$category->category_slug = Input::get('category_slug');
// other fields (if have any)
$category->save();
Now create Post
and attach categories:
$post = new Post; // Assume that, this is the first post so id would be 1
$post->title = 'My Post'; // Input::get('post_title');
$post->slug = 'mypost'; // Input::get('post_slug');
// Assign other values like post_content etc then
$post->save();
Once the Post
is saved then:
// Attach two categories with this post using category id
$post->categories()->sync(array(1, 2)); // These (1, 2) are category ids
So, now you have a Post
and this Post
belongs to to categories, in other words, this post has been created under two categories by syncing the the post and categories, you are actually making a relation between the post and two categories and these relational data will be saved in category_post
table so according to this example you category_post
table will contain something like this:
id | category_id | post_id
----------------------------
1 | 1 | 1
2 | 2 | 1
Now you may query
using Post
model to get all the posts with categories like this:
$posts = Post::with('categories')->get();
Also find a single post by id with categories related to it using something like this:
$post = Post::with('categories')->find(1);
You may access the related categories using this:
$post->categories->get(0); // first category from the collection
$post->categories->get(1); // second category from the collection
If you pass the collection of Post
models to your view like this;
$posts = Post::with('categories')->get();
return View::make('post.index')->with('posts', $posts);
Then in the view you may loop all posts and categories like this:
@foreach($posts as $post)
{{ $post->post_title }}
{{ $post->post_content }}
@foreach($post->categories as $category)
{{ $category->title }}
@endforeach
@endforeach
You may also use the Category
model like:
// Get all categories with related posts
$categories = Category::with('posts')->get();
// Get a category using it's id with related posts
$category = Category::with('posts')->find(2); // Category id 2
This is the basic idea, read the manual (Eloquent ORM) for more.