user10502851
user10502851

Reputation:

View Not Found While Trying To Use Route Model Binding

I want to edit users so I added this as link:

<a href="{{ route('users.edit', $user->name) }}" class="on-default edit-row">Edit</a>

And at the Controller:

public function edit(User $user)
    {
        $roles = Role::where('slug', '!=', 'super-admin')->get();
        return view('admin.users.edit', compact('roles', 'user'));
    }

So as you can see I have called the $user from User Model but now the problem is, it says:

404 | NOT FOUND

However if I remove User $user to $id, it will be working fine !

So what is going wrong here ? How can I properly use Route Model Binding here ?

I would really appreciate any idea or suggestion from you guys...

Thanks in advance.

Upvotes: 0

Views: 70

Answers (1)

NICO
NICO

Reputation: 1843

You have to pass the user's id instead of the users name to your route.

<a href="{{ route('users.edit', $user->id) }}" class="on-default edit-row">Edit</a>

Otherwise, if you want to use the user's name you have two options

  1. Add getRouteKeyName to user.php
public function getRouteKeyName()
{
    return 'name';
}
  1. Customize implicit route model binding directly in your web.php
Route::get('/users/{user:name}/edit', function (User $user) {
    // ...
});

Upvotes: 1

Related Questions