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, (example.com/posts/all-about-my-new-banana-maker), but to be able to call the edit route by using the UUID (example.com/posts/ddd83f7b-9c73-11eb-93c2-e55e28ace783/edit)
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?

Answers:

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="https://getridbug.com/cdn-cgi/l/email-protection" 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 stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x