Skip to main content

BlogController

The BlogController handles blog post management operations in the admin panel, including CRUD operations for posts with support for categories, tags, and images. Namespace: App\Http\Controllers\BlogController

index()

Display a paginated listing of all blog posts in the admin panel. Route: GET /admin/blog/posts (admin.blog.posts) Returns: View - Admin blog posts listing page Example:
// Returns view with paginated posts
$posts = Post::query()
    ->paginate(10)
    ->withQueryString();
return view('admin.blog.posts', ['posts' => $posts]);
Source: BlogController.php:26

create()

Show the form for creating a new blog post. Route: GET /admin/blog/create (admin.blog.create)
post
Post
Post model instance for authorization
Authorization: Requires create permission on Post model Returns: View - Blog post creation form with categories and tags Example:
return view('admin.blog.create', [
    'blogCategories' => BlogCategory::all() ?? collect(),
    'tags' => Tag::all() ?? collect()
]);
Source: BlogController.php:38

store()

Store a newly created blog post in storage. Route: POST /admin/blog/posts (admin.blog.store)
request
Request
required
HTTP request containing post data
post
Post
Post model instance
Validation Rules:
  • title: required, unique, min:8, max:255
  • post_image: file (optional)
  • body: required
  • slug: required, unique in posts table
  • category_id: nullable, must exist in blog_categories
  • tags: nullable array
  • tags.*: must exist in tags table
Authorization: Requires create permission on Post class Returns: RedirectResponse - Redirects to admin blog posts list Example:
$validated = $request->validate([
    'title' => 'required|unique:posts|min:8|max:255',
    'post_image' => 'file',
    'body' => 'required',
    'slug' => 'required|unique:posts,slug',
    'category_id' => 'nullable|exists:blog_categories,id',
    'tags' => 'nullable|array',
    'tags.*' => 'exists:tags,id'
]);

$validated['user_id'] = auth()->id();

if ($request->hasFile('post_image')) {
    $validated['post_image'] = Storage::disk('public')
        ->put('images', $request->file('post_image'));
}

$post = Post::create($validated);
$post->tags()->sync($validated['tags'] ?? []);

session()->flash('message', 'Post Created Successfully.');
return redirect()->route('admin.blog.posts');
Source: BlogController.php:63

edit()

Show the form for editing a specific blog post. Route: GET /admin/blog/posts/{post}/edit (admin.blog.edit)
post
Post
required
The post instance to edit (route model binding)
Returns: View - Blog post edit form with post data, categories, and tags Example:
return view('admin.blog.edit', [
    'post' => $post,
    'blogCategories' => BlogCategory::all() ?? collect(),
    'tags' => Tag::all() ?? collect()
]);
Source: BlogController.php:51

update()

Update a specific blog post in storage. Route: PATCH /admin/blog/posts/{post}/update (admin.blog.update)
request
Request
required
HTTP request containing updated post data
post
Post
required
The post instance to update (route model binding)
Validation Rules:
  • title: required, min:8, max:255, unique except current post
  • post_image: file (optional)
  • body: required
  • category_id: nullable, must exist in blog_categories
  • tags: nullable array
  • tags.*: must exist in tags table
Authorization: Requires update permission on the post Returns: RedirectResponse - Redirects to admin blog posts list Example:
$validated = $request->validate([
    'title' => 'required|min:8|max:255|unique:posts,title,' . $post->id,
    'post_image' => 'file',
    'body' => 'required',
    'category_id' => 'nullable|exists:blog_categories,id',
    'tags' => 'nullable|array',
    'tags.*' => 'exists:tags,id'
]);

if ($request->hasFile('post_image')) {
    $validated['post_image'] = Storage::disk('public')
        ->put('images', $request->file('post_image'));
    $post->post_image = $validated['post_image'];
}

$post->update($validated);
$post->tags()->sync($validated['tags'] ?? []);

session()->flash('message', 'Post Updated Successfully.');
return redirect()->route('admin.blog.posts');
Source: BlogController.php:104

show()

Display a specific blog post by slug. Route: GET /admin/blog/posts/{post} (admin.blog.show)
slug
string
required
The unique slug identifier for the post
Returns: View - Blog post detail page Example:
$post = Post::where('slug', $slug)->firstOrFail();
return view('blog.show', compact('post'));
Source: BlogController.php:94

destroy()

Remove a specific blog post from storage. Route: DELETE /admin/blog/posts/{post} (admin.blog.destroy)
post
Post
required
The post instance to delete (route model binding)
Authorization: Requires delete permission on the post Returns: RedirectResponse - Redirects back to previous page Example:
$post->delete();
session()->flash('message', 'Post Deleted Successfully.');
return back();
Source: BlogController.php:133

PostController

The PostController handles public-facing blog post display operations for the frontend. Namespace: App\Http\Controllers\PostController

index()

Display a paginated listing of published blog posts on the public blog page. Route: GET /blog (blog.index) Returns: View - Public blog index with paginated posts Example:
$posts = Post::orderByDesc('published_at')->paginate(8);
return view('blog.index', compact('posts'));
Source: PostController.php:11

show()

Display a specific blog post by slug on the public blog. Route: GET /blog/{slug} (blog.show)
slug
string
required
The unique slug identifier for the post
Returns: View - Public blog post detail page Throws: ModelNotFoundException if post not found Example:
$post = Post::where('slug', $slug)->firstOrFail();
return view('blog.show', compact('post'));
Source: PostController.php:18

Build docs developers (and LLMs) love