Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate route model binding resolution #115

Closed
faustbrian opened this issue Aug 4, 2019 · 4 comments

Comments

@faustbrian
Copy link
Contributor

commented Aug 4, 2019

Tried Livewire today and enjoying it so far but ran into an odd issue that required me to make quite a few changes to my application.

When you set models or closures that should be used for route model binding resolutions Livewire will run substituteBindings which has already been executed by Laravel itself. This results in Laravel trying to resolve the model with a wrong value in the second run.

Flow

resolve bindings (laravel) > use id > get user model > resolve bindings (livewire) > use user model > error because user model is used instead of ID

The user model is used in the second run that is fired by Livewire because by that time Laravel has already resolved and bound the instance so the string that used to be /{user} in the route no longer exists.

Code to reproduce

// routes/web.php
Route::livewire('/users/{user}', 'show-user');

// RouteServiceProvider
Route::model('user', User::class);

// Component
namespace App\Http\Livewire;

use App\User;
use Livewire\Component;

class ShowUser extends Component
{
    public $user;

    public function mount(User $user)
    {
        $this->user = $user;
    }
}

Current Solution
My solution as of right now is to use a getModel method to get the model that is being worked on and avoid using route model bindings and injection with Livewire.

public function getModel(): User {
    return request('user');
}
@calebporzio

This comment has been minimized.

Copy link
Owner

commented Aug 5, 2019

Ok, I'm looking into this problem. But can't you just remove the Route::model('user', User::class);?

Livewire will automatically recognize and bind the proper model using only the typehint in the method signature?

@faustbrian

This comment has been minimized.

Copy link
Contributor Author

commented Aug 5, 2019

There are other parts of that application that automatically grab resolved models from the request()->route() bindings, like for example breadcrumbs, so those would have to be changed to manually grab those models instead of having them auto-resolved by Laravel on every request.

@calebporzio

This comment has been minimized.

Copy link
Owner

commented Aug 5, 2019

Ok, I fixed the issue. Should be solved in the latest release (v0.1.5)

@calebporzio calebporzio closed this Aug 5, 2019

@faustbrian

This comment has been minimized.

Copy link
Contributor Author

commented Aug 5, 2019

Great, thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.