michaeldyrynda / laravel-model-uuid

This package allows you to easily work with UUIDs in your Laravel models
MIT License
442 stars 46 forks source link

Ability to bind on desired uuid column from model #106

Closed afshiney closed 2 years ago

afshiney commented 2 years ago

When used on model, the added trait adds the ability to route bind the model to desired uuid column, As an example:

Route::get('users/{user}', function (User $user){
    return $user;
});

Above code would bind on default 'uuid' column name and perform under "whereUuid" scope.

Also in case model has more than one uuid column and/or any name other than 'uuid' is set for column, the added trait feeds the custom column name to responsible scope:

public function uuidColumns(): array
{
    return ['uuid', 'other_uuid_column'];
}

Route::get('users/{user:other_uuid_column}', function (User $user){
    return $user;
});

As usual custom columns should be registered within "uuidColumns" function.

michaeldyrynda commented 2 years ago

Any chance we can add some tests to verify the behaviour? i.e. that the binding works on the default column, a custom value, and that it correctly returns a 404 when the model cannot be found?

afshiney commented 2 years ago

Of course, just pushed the latest commit that contains the required tests.

michaeldyrynda commented 2 years ago

I added a getRouteKeyName(): string method to the BindsOnUuid trait as well. If we're going to support implicit binding, we should support it on the named routes as well. Readme updated to reflect this change, too.

I also tweaked the it_fails_on_invalid_uuid_when_custom_route_key_used test as it looked like it had been copy/pasted from the test above but not updated.