Laravel Route model binding - empty user

Viewed 5684

I have a route

http://192.168.10.15/user/33/edit

I am trying to return the user based on the url id.

  public function edit($id, \App\User $user)
    {
        dd($user->id);
         return view('user.update');

    }

The id is returning null, how do I do this?

3 Answers

If you recently upgraded to laravel 5.3 and above, you might want to check if you have updated laravel/app/Http/Kernel.php to register route middleware for binding substitution.

Route model binding is now accomplished using middleware. All applications should add the Illuminate\Routing\Middleware\SubstituteBindings to your web middleware group in your app/Http/Kernel.php file:

\Illuminate\Routing\Middleware\SubstituteBindings::class,

You should also register a route middleware for binding substitution in the $routeMiddleware property of your HTTP kernel:

'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,

Once this route middleware has been registered, you should add it to the api middleware group:

'api' => [
    'throttle:60,1',
    'bindings',
],

I fixed it by adding named route. Model binding didn't work if there was no name specified for route.

Route::get('product/{product}', "ProductController@read")->name('product.read');
Related