I want to use the following URL: mydomen.ru/user/username
User Model:
namespace App; use Illuminate\Auth\Authenticatable; use Illuminate\Database\Eloquent\Model; use Illuminate\Auth\Passwords\CanResetPassword; use Illuminate\Foundation\Auth\Access\Authorizable; use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract; class User extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract { use Authenticatable, Authorizable, CanResetPassword; /** * The database table used by the model. * * @var string */ protected $table = 'user'; /** * The attributes that are mass assignable. * * @var array */ protected $fillable = ['name', 'email', 'password']; /** * The attributes excluded from the model's JSON form. * * @var array */ protected $hidden = ['password', 'remember_token']; public function getRouteKey() { return $this->name; }
}
The route looks like this:
Route::get('user/{user}', 'UserController@show');
This is the controller:
<?php namespace App\Http\Controllers; use App\User; use Illuminate\Http\Request; use App\Http\Requests; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\App; class UserController extends Controller { /** ... */ /** * Display the specified resource. * * @param User $user * @return \Illuminate\Http\Response */ public function show(User $user) { return view('user/show', ['user'=>$user->toJson()] ); } /** ... */ }
This is in the RouteServiceProvider:
public function boot(Router $router) { $router->model('user', 'App\User'); parent::boot($router); }
What did I miss? Why is the URL still linked to user.id
and not to user.name
, as I want?