How could I have 2 different route keys names for my Post model? (Laravel)

I’ve made a simple Laravel CRUD, and I’m stuck on a problem.
My posts have a slug column and a uuid column in the table in which they are stored. I’ve binded the routes to the PostController using

Route::resource("posts", "PostController");

I want to be able to call the show route by using the slug in the URL, (, but to be able to call the edit route by using the UUID (
I’ve tried editing my Post model and changing getRouteKeyName:

class Post extends Model
    use HasFactory;

    // Set mass-assignable fields
    protected $fillable = ["title", "content", "category", "image", "slug", "uuid"];

     * Get the route keyfor the model
     * @return string
    public function getRouteKeyName() {
        return "slug";

I can only see to have UUID or slug, not both at the same time. How could I make it so that both UUID and slug retrieve my post?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

If you want to pass extra route parameter, then you need to define an another route for that :

Route::get("posts/{uuid}/{slug}", "<a href="" class="__cf_email__" data-cfemail="d787b8a4a394b8b9a3a5b8bbbbb2a597b3b6a3b6">[email protected]</a>");

And your controller will be looked like :

public function data($uuid, $slug) {
    // You can access $uuid and $slug here

Method 2

You can either remove any type hints from your methods, or override the route model binding in your RouteServiceProvider.

Remove type hinting

public function show($post)
    $post = Post::where('slug', $post)->firstOrFail();

public function edit($post)
    $post = Post::where('uuid', $post);

Override route model binding

Add something like the following to the boot method of the RouteServiceProvider.

Route::bind('post', function ($value){
    return Post::where('slug', $value)->orWhere('uuid', $value)->firstOrFail();

You still use type hinting if you override the route model binding in the RouteServiceProvider.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x