Blog post display in laravel based on category click
//web.php
Route::get('/view-category/{slug}', [FrontEndFrontEndController::class, 'viewCategoryPost']);
//controller code
public function viewCategoryPost($slug)
{
$category = Category::where('slug', $slug)->first();
if (!$category) {
return redirect('/blog')->with('status', 'Category not found');
}
$posts = $category->posts;
return view('layouts.frontend.posts.index', compact('category', 'posts'));
}
//category lists blade file code
<div class="post-category">
<a href="#" class="active">view all</a>
@foreach ($categories as $item)
<a href="{{url('view-category/'.$item->slug)}}">{{$item->name}}</a>
@endforeach
</div>
No comments