7

In my routes.php I have this:

Route::get('user/{user}/permissions/','UserController@permissions')->name('user.permissions');

In my controller I have:

public function permissions(User $user){
   dd($user);
}

$user is empty object (like new user; without attributes)

if I use:

public function permissions($user){
   dd(User::find($user));
}

Works perfectly!!

I have previously Laravel 5.2 and this code works fine but in Laravel 5.5 it doesn't work, any ideas why?

Alexander O'Mara
  • 58,688
  • 18
  • 163
  • 171
Abel Olguin Chavez
  • 1,350
  • 1
  • 12
  • 24
  • you must ensure that the ID that you past to your URL (example : `user/1/permission/`in your database there's a user with that ID else you will get a 404 HTTP Response – Yves Kipondo Dec 13 '17 at 01:17
  • was this an upgrade? – lagbox Dec 13 '17 at 01:20
  • Could it be that you are missing `use App\User;` at the top. If this is the case the `User` it is expecting is in the wrong namespace. – Michael Dec 13 '17 at 01:22

1 Answers1

17

Sounds like you upgraded from 5.2 up to ... some version.

Laravel 5.3 uses the SubstitueBindings middleware to do the implicit and explicit bindings, it is no longer done via the router before the middleware stack.

If you upgraded and did not add this middleware to any of the groups, you will not have your route model bindings as the middleware is responsible for substituting the parameter with the binding.

"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, ..."

Laravel 5.3 Docs - Upgrade - Middleware - Binding Substitution Middleware

lagbox
  • 48,571
  • 8
  • 72
  • 83