Juliatzin
Juliatzin

Reputation: 19705

Custom Model and fields with Sentinel / Laravel

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!

Upvotes: 0

Views: 1277

Answers (1)

patricus
patricus

Reputation: 62338

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

Upvotes: 2

Related Questions