【问题标题】:Custom Model and fields with Sentinel / Laravel使用 Sentinel / Laravel 自定义模型和字段
【发布时间】:2016-04-01 21:42:57
【问题描述】:

I',将新系统集成到现有数据库中。

所以,我的用户表没有默认字段名称。

所有名称都是西班牙语,因此 Sentinel 在应该查找“correo”时会查找电子邮件

另外,在执行时

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

我找到的唯一资源是一个非常快的资源:

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

上面说非常简单,但不要提这个案例。

那么,基本上,如何自定义 Sentinel 模型的所有字段名称???

这是我的模型:

class Administrador extends EloquentUser {

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

protected $dates = ['deleted_at'];

}

任何帮助将不胜感激!

【问题讨论】:

    标签: php laravel laravel-5 eloquent sentinel


    【解决方案1】:

    首先,id 问题是一个基本的 Laravel Eloquent 问题。如果您的模型的主键不是id,那么您需要将模型上的$primaryKey 属性设置为正确的字段名称。此外,如果您的主键不是自增整数,那么您还需要将$incrementing 属性设置为false

    对于 email 问题,这是 Sentinel 特定的问题。 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'];
    }
    

    【讨论】:

    猜你喜欢
    • 2019-09-15
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多