Overview
The Post model represents blog posts in LaraCMS. It supports categorization through blog categories, tagging system, and maintains author relationships.
Namespace: App\Models\Post
Extends: Illuminate\Database\Eloquent\Model
Properties
Fillable Attributes
The ID of the user who created the post (author)
The main content of the post
URL-friendly version of the title for routing
Path or URL to the post’s featured image
Relationships
user()
Type: belongsTo
Related Model: App\Models\User
Description: Returns the user (author) who created this post.
public function user()
{
return $this->belongsTo(User::class);
}
Example:
$post = Post::find(1);
$author = $post->user;
echo $author->name; // "John Doe"
blogCategories()
Type: belongsToMany
Related Model: App\Models\BlogCategory
Pivot Table: blog_category_post
Description: Returns all blog categories assigned to this post. A post can belong to multiple categories.
public function blogCategories()
{
return $this->belongsToMany(BlogCategory::class, 'blog_category_post');
}
Example:
$post = Post::find(1);
$categories = $post->blogCategories;
foreach ($categories as $category) {
echo $category->name;
}
Type: belongsToMany
Related Model: App\Models\Tag
Pivot Table: post_tag (Laravel convention)
Description: Returns all tags assigned to this post. Posts can have multiple tags for better organization and searchability.
public function tags()
{
return $this->belongsToMany(Tag::class);
}
Example:
$post = Post::find(1);
$tags = $post->tags;
foreach ($tags as $tag) {
echo $tag->name;
}
Usage Examples
Creating a Post
use App\Models\Post;
$post = Post::create([
'user_id' => auth()->id(),
'title' => 'Getting Started with Laravel',
'body' => 'This is a comprehensive guide...',
'slug' => 'getting-started-with-laravel',
'post_image' => '/images/laravel-guide.jpg',
]);
Assigning Categories
$post = Post::find(1);
// Attach single category
$post->blogCategories()->attach($categoryId);
// Attach multiple categories
$post->blogCategories()->attach([1, 2, 3]);
// Sync categories (replaces all existing)
$post->blogCategories()->sync([1, 2, 3]);
// Detach category
$post->blogCategories()->detach($categoryId);
$post = Post::find(1);
// Attach tags
$post->tags()->attach([1, 2, 3]);
// Sync tags
$post->tags()->sync([1, 2, 3, 4]);
// Get tag names
$tagNames = $post->tags->pluck('name')->toArray();
Retrieving Posts with Relationships
// Eager load relationships
$posts = Post::with(['user', 'blogCategories', 'tags'])->get();
// Get posts by category
$category = BlogCategory::find(1);
$posts = $category->posts;
// Get posts by tag
$tag = Tag::find(1);
$posts = $tag->posts;
// Get posts by author
$user = User::find(1);
$posts = $user->posts; // Note: This requires a posts() relationship in User model
Filtering and Searching
// Find post by slug
$post = Post::where('slug', 'getting-started-with-laravel')->first();
// Get recent posts
$recentPosts = Post::orderBy('created_at', 'desc')
->limit(10)
->get();
// Search posts by title
$posts = Post::where('title', 'like', '%Laravel%')->get();
// Get posts with specific category
$posts = Post::whereHas('blogCategories', function($query) {
$query->where('name', 'Tutorial');
})->get();
// Get posts with specific tag
$posts = Post::whereHas('tags', function($query) {
$query->where('name', 'PHP');
})->get();
Updating Posts
$post = Post::find(1);
$post->update([
'title' => 'Updated Title',
'body' => 'Updated content...',
'slug' => 'updated-title',
]);
// Update with categories
$post->update(['title' => 'New Title']);
$post->blogCategories()->sync([2, 3]);
$post->tags()->sync([1, 4, 5]);
Database Schema
The posts table includes:
id - Primary key
user_id - Foreign key to users table
title - Post title
body - Post content (text)
slug - URL-friendly slug
post_image - Featured image path
created_at - Timestamp
updated_at - Timestamp
Pivot Tables
blog_category_post:
blog_category_id - Foreign key to blog_categories
post_id - Foreign key to posts
post_tag:
post_id - Foreign key to posts
tag_id - Foreign key to tags