2016-04-01 29 views
0

I',將新系統集成到現有數據庫。自定義模型和字段與哨兵/ Laravel

所以,我的用戶表沒有默認的字段名稱。

所有的名字都在西班牙,因此,執行

Sentinel::check(), 

我得到這個消息的錯誤時,哨兵查找電子郵件時,他應該找「郵報」

而且,:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'administrador.id' in 'where clause' (SQL: select * from `administrador` where `administrador`.`id` = 1 and `administrador`.`deleted_at` is null limit 1) 

其實id不存在,這個PK叫做administradorid

唯一的reso urce我發現是一個非常快的一個:

https://github.com/cartalyst/sentinel/wiki/Extending-Sentinel

它說,它是很容易的極端,但沒有提到這種情況。

那麼,基本上,我該如何定製Sentinel Model的所有字段名?

這裏是我的模型:

class Administrador extends EloquentUser { 

protected $table = 'administrador'; 
protected $fillable = []; 
protected $guarded = ['administradorid']; 
protected $hidden = ['contrasena', 'remember_token']; 
use SoftDeletes; 

protected $dates = ['deleted_at']; 

} 

任何幫助將不勝感激!

回答

1

首先,id問題是一個基本的Laravel雄辯問題。如果您的模型的主鍵不是id,那麼您需要將模型上的$primaryKey屬性設置爲正確的字段名稱。此外,如果您的主鍵不是自動遞增整數,那麼您還需要將$incrementing屬性設置爲false

對於email問題,這是一個哨兵特定問題。 EloquentUser類具有$loginNames屬性,該屬性設置爲包含用戶登錄的有效字段名稱的數組。缺省值僅爲['email'],所以您需要覆蓋此屬性並將其更改爲字段名稱。

所以,你Administrador類最終看起來像:

class Administrador extends EloquentUser { 
    use SoftDeletes; 

    protected $table = 'administrador'; 
    protected $primaryKey = 'administradorid'; 
    //public $incrementing = false; // only if primary key is not an autoinc int 

    protected $fillable = []; 
    protected $guarded = ['administradorid']; 
    protected $hidden = ['contrasena', 'remember_token']; 
    protected $dates = ['deleted_at']; 

    // Sentinel overrides 

    // array of fields that hold login names 
    protected $loginNames = ['correo']; 
} 
+1

你重新rightttt我的壞PK ......我會嘗試週一告訴你! TX! –

+0

它的工作原理。下一章,哈希方法是不一樣的 - > http://stackoverflow.com/questions/36407296/hash-password-compatibility-between-yii-php-crypt-and-laravel-sentines –