我正在阅读Laravel 5文档,在理解路由模型绑定时遇到了一些问题。
我使用here中的示例代码
所以,我在RouteServiceProvider.php中添加了一行:
public function boot(Router $router)
{
parent::boot($router);
$router->model('user', 'App\User');
}我添加了路由:
Route::get('/profile/{user}', function(App\User $user)
{
die(var_dump($user));
});使用默认的Laravel用户模型。
<?php namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
use Authenticatable, CanResetPassword;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* 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'];
}我有MySQL表'users‘。当我转到URL http://blog.app/profile/1时,我希望看到ID为1的用户的数据,但我不知道如何获得实际的模型值。相反,我看到:

有没有什么特殊的方法来获取模型值?还是我错过了什么?
发布于 2015-02-11 17:08:22
我这里也有同样的问题。你只得到了一个空的App\User实例,因为你在你的Route::get()中声明了它。永远不会加载模型。
将模型绑定到参数的另一种方法:
Route::bind('user', function($value)
{
return App\User::find($value);
});传递给bind方法的闭包将接收URI段的值,并且应该返回您希望注入到路由中的类的一个实例。
发布于 2015-05-03 17:17:35
我也遇到过同样的问题。
在"RouteServiceProvider“中查找"$namespace”变量,并尝试将其设置为空:
protected $namespace = '';https://stackoverflow.com/questions/28446494
复制相似问题