Custom Model and fields with Sentinel / Laravel

删除回忆录丶 提交于 2019-12-25 08:47:43

问题


I', integrating a new system to an existing database.

So, my User table doesn't have default fields names.

All names are in spanish, so, Sentinel looks for email when he should look for "correo"

Also, when executing

Sentinel::check(), 

I get this message error:

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)

In fact, id doesn't exists, the PK is called administradorid

The only resource I found is a very quick one:

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

It says it is extremly easy, but do not mention this case.

So, basically, how can I customize all the fields name of the Sentinel Model???

Here is my model:

class Administrador extends EloquentUser {

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

protected $dates = ['deleted_at'];

}

Any help will be appreciated!


回答1:


First, the id issue is a basic Laravel Eloquent issue. If the primary key for your model is not id, then you need to set the $primaryKey property on your model to the correct field name. Additionally, if your primary key is not an autoincrementing integer, then you need to set the $incrementing property to false, as well.

For the email issue, that is a Sentinel specific issue. The EloquentUser class has a $loginNames property that is set to an array of valid field names that contain user logins. The default is just ['email'], so you need to override this property and change it to your field name.

So, your Administrador class ends up looking like:

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'];
}


来源:https://stackoverflow.com/questions/36366306/custom-model-and-fields-with-sentinel-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!