Laravel 5文档 - 路由模型绑定
问题描述:
我正在阅读Laravel 5文档,并在理解路由模型绑定时遇到了一些问题。Laravel 5文档 - 路由模型绑定
我使用的示例代码从here
所以,我添加一行RouteServiceProvider.php:
public function boot(Router $router)
{
parent::boot($router);
$router->model('user', 'App\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的用户的数据,但我不明白如何获取实际模型值。相反,我看到:
有没有得到模型值任何特殊的方法?或者我错过了什么?
答
我在这里有同样的问题。由于您在Route::get()
中声明了这个实例,因此您只会获得一个空的实例App\User
。该模型从不加载。
绑定模型参数的另一种方法:
Route::bind('user', function($value)
{
return App\User::find($value);
});
传递给bind方法的闭幕会收到URI段的值,并且应该返回你想要的类的实例被注入进入路线。
答
我有同样的问题。
寻找 “$命名空间” VAR在 “RouteServiceProvider”,并尝试将其设置为空:
protected $namespace = '';
如果你'DD($用户>名称)' - 那是什么节目? – Laurence 2015-02-11 05:53:38
实际上,@TheShiftExchange有一个好处,因为模型看起来像是正确加载的。 – manix 2015-02-11 06:35:40
我得到了完全相同的问题。我向Laravel汇报。它在几天前工作! – 2015-02-26 23:39:25