codemode
2 years ago

Using a url slug and id on the route

Posted 2 years ago by codemode

Hello,

I'm trying to put the slug and id on the URL.

So, i have a Post model, with basic things like id, title, description. (example - 1 , first-title, first description)... and i want the URL to look like www.website.com/first-title_1

Route -

Route::get('{slug}_{id}', '[email protected]');

Controller -

public function show(Post $id, $slug)
    { 
        return $id; //or compact into a view
              // and use str_slug() on the $title
    }

I'm not sure if i need to store the slug into the database, also i'm not sure how the passing of values for $id and $slug should be.

Any help please? Thanks!

Please sign in or create an account to participate in this conversation.