2015-02-11 210 views
3

我正在閱讀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的用戶的數據,但我不明白如何獲取實際模型值。相反,我看到:

enter image description here

有沒有得到模型值任何特殊的方法?或者我錯過了什麼?

+1

如果你'DD($用戶>名稱)' - 那是什麼節目? – Laurence 2015-02-11 05:53:38

+0

實際上,@TheShiftExchange有一個好處,因爲模型看起來像是正確加載的。 – manix 2015-02-11 06:35:40

+0

我得到了完全相同的問題。我向Laravel彙報。它在幾天前工作! – 2015-02-26 23:39:25

回答

4

我在這裏有同樣的問題。由於您在Route::get()中聲明瞭這個實例,因此您只會獲得一個空的實例App\User。該模型從不加載。

綁定模型參數的另一種方法:

Route::bind('user', function($value) 
{ 
    return App\User::find($value); 
}); 

傳遞給bind方法的閉幕會收到URI段的值,並且應該返回你想要的類的實例被注入進入路線。

0

我有同樣的問題。
尋找 「$命名空間」 VAR在 「RouteServiceProvider」,並嘗試將其設置爲空:

protected $namespace = '';