Implicit Model Binding
For those who are looking for the Laravel 5.3+ answer:
Jeffery Way talked about this in one of his laracasts; not sure which one though.
Basically, you can pass the username through a route like this:
// you will need to set this up so the route ('/user/{user}')
// matches the Model you're binding to -> App\<User>
// ('/user/{username}') will not work
Route::get('/user/{user}', function(App\User $user){
// $user should now be App\User
return view('user.show', compact('user'));
// access the user in the 'user.show' view
// like: $user->username
});
However, to make this work, you need to add this to the User class:
public function getRouteKeyName()
{
return 'username';
}
Otherwise this would only work if you passed the id of the user in place of the username -> /user/1