Hierophect's avatar

Can't get route model binding to work

I've got a really simple question. I've got the following route: Route::get('/users/{user}', 'UsersController@show'); It's the first in the list.

I've got the following function in UsersController:

<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;

class UsersController extends Controller
{

    public function show(User $user) 
    {
        return "hello world";
        return view('profiles.show', [
            'profileUser'=> $user,
            'jobs'=> $user->jobs()->paginate(10)
        ]);
    }
}

The User model is the usual one that comes with Auth.

This route always returns "sorry, the page you are looking for could not be found". Every time. The route, controller, and controller method are all identical to one in a similar route already in the app that works just fine.

Why isn't route model binding working in this particular instance? Is there some laravel wizardry that I'm not familiar with messing with this?

0 likes
13 replies
tykus's avatar

401 is not a NOT FOUND response status; it would suggest that the route is protected by the auth middleware and the request is by a guest.

1 like
Hierophect's avatar

@tykus you are right but sadly that was just a typo on my part and not my actual error :( I've updated the question to reflect that. My user is signed in on the web app.

Cronix's avatar

That would suggest that the user id requested doesn't exist. Route model binding uses findOrFail() when retrieving, so if it's not found, it will produce a 404 error.

Like going to http:://yoursite.com/users/4 ... user with id of 4 doesn't exist.

Hierophect's avatar

If I replace the url with "jobs/{job}" I can at least get it into the function, so it definitely has to do with the route model binding of the User model. Is that prohibited or something?

Cronix's avatar

Are you trying to retrieve the user, or the job, by the passed in id? You're retrieving a user.

Hierophect's avatar

I'm retrieving a user. I'm referencing the fact that the route model binding is only failing for the User model. Even the following doesn't work.

Route::get('/users/{user}', function (App\User $user) {
    return "but why";
});

This is practically copy pasted off the Laravel website for route model binding so I'm pretty confused.

Hierophect's avatar

I'm also quite certain the ID is not the problem.

Cronix's avatar

What does this return?

Route::get('/users/{user}', function ($user) {
    dump($user);
    dump(App\User::find($user));
});

And does the id exist for the user being requested?

1 like
Hierophect's avatar

That works fine. I can use that in the meantime, I was just hoping to figure out why the route model binding for User isn't working like it does for all my other models.

Cronix's avatar

If that works, I'm just as confused as you are. I was just trying to narrow it down to where it's actually failing.

Is this route all by itself in your routes file, or is it within a group or something? Is there any extra middleware being used?

Hierophect's avatar

Nope. Only middleware I'm using is the auth() scaffold. User is in the usual place, too, next to all my other models in App. Honestly, my best guess is that there's some kind of laravel magic associated with User that makes it nonviable for route model binding? Honestly I'm just not sure.

tykus's avatar
tykus
Best Answer
Level 104

Have you done anything with the key for the User model, e.g. overriding the getRouteKeyName method on the User model?

1 like
Hierophect's avatar

Yep that was it. I'd left over this function from the Laravel forum tutorials:

     public function getRouteKeyName()
     {
         return 'name';
     }

I'd gotten rid of everything else since I'd gotten tired of the fact the series kept using the users non-unique name in the URL, but I'd missed this bit. Thanks for helping me find it!

Please or to participate in this conversation.