Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dane5890's avatar

How Can I Show The Category Name Dynamically In Blog Slug

Hey, I'm putting together a blog in Laravel 8. So far so good. I can create categories, sub categories, sub sub categories, and blogs no problem. The question I have is, how can I dynamically show the specific category dynamically in each blog post's slug? For my blog slug my controller route reads as

Route::get('/natural-hair-blogs/{id}/{slug}', [HairBlogController::class, 'Show'])->name('blogs.show');

I'm not sure how to go about it. Any help is appreciated.

0 likes
6 replies
samehdev's avatar
     Route::get('/p/{category}/{subcategoryOrSlug?}/{slug?}', [HomeController::class, "dynmicurl"])->name('dynmicurl.index');

Controller

  public function dynmicurl(Request $req, $category, $subcategoryOrSlug= null, $slug = null)
    {
            //Here you set your conditions to know if you should bring only the category, article or subcategory
     }
dane5890's avatar

@samehdev It now appears that I'm having issues dynamically displaying the slug from the index page. I get this error

Missing required parameter for [Route: mainblogs.show] [URI: natural-hair-blogs/d/{slug}] [Missing parameter: slug]

But when I enter the slug manually, I can get the dynamic blog post. Here is my code.

<h2> <a href="{{ route('mainblogs.show', $blog->slug) }}"> {{ $blog->title }} </a> </h2>

This is really strange

Snapey's avatar

add them to the url, but you need to pass them wherever you create a link to the route

Route::get('/natural-hair-blogs/{category}/{subcat}/{id}/{slug}', [HairBlogController::class, 'Show'])->name('blogs.show');

accept them in the controller but you don't need to do anything with them

ehab.aboshehab's avatar

@dane5890 If you just want to show them in the url then you can do something like this :

Route::get('natural-hair-blogs/{category}/{sub_category}/{id}/{slug}', [HairBlogController::class, 'Show'])->name('blogs.show');

If you want to use the categories or the sub categories later in the future you will need to pass the ids also :

Route::get('natural-hair-blogs/{category}-{category_id}/{sub_category}-{sub_category_id}/{id}/{slug}', [HairBlogController::class, 'Show'])->name('blogs.show');
dane5890's avatar

@ehab.aboshehab I'm afraid the suggestion doesn't work. Not sure why. Here is my adjusted controller code. If I'm wrong, please point it out


public function NaturalSubShow($category, $slug) {

$categories = HairBlogCategory::where('slug', $slug)->first();
$subcategories = HairBlogSubCategory::where('slug', $slug)->first();
return view('natural-hair-blogs.subcat.show', compact('subcategories','categories'));

}

In my route web I have it adjusted like

Route::get('/natural-hair-blogs/s/{category}/{id}/{slug}', [HairBlogCategoryController::class, 'NaturalSubShow'])->name('natural.subcategories.show');


And in my blade I have it like this. I know there is something wrong with my code, but I don't what.

{{ URL::to('/natural-hair-blogs/s').'/'.$category->slug.'/'.$subcategory->slug }}"

Please or to participate in this conversation.