mardi 9 mai 2017

Laravel Model without a Database?

I'm developing a system in which i have different roles for accesing. The role is present in the Users table as an integer value.

I've created a model called roles and in this model i dont need a database connection or a table. I did set some constant values representing roles matching the role field in the user table.

So what's the problem ? It seems that i can't have relations with the roles model if this hasn't a table associated in the database.

This is my roles model:

class Roles extends Model {

    const ROL_ADMINISTRADOR = 1;
    const ROL_DIRECTOR = 2;
    const ROL_PROFESOR = 3;
    const ROL_RECOPILADOR = 4;

    private $rol_id;
    private $roles = [
        self::ROL_ADMINISTRADOR => 'Administrador',
        self::ROL_DIRECTOR => 'Director',
        self::ROL_PROFESOR => 'Profesor',
        self::ROL_RECOPILADOR => 'Recopilador'
    ];

    /**
     * RELACIONES
     */
    public function _usuarios() {
        return $this->hasMany(Usuarios::class, "rol", "rol_id");
    }

    /**
     * FORMATEADORES
     */

    /**
     * FUNCIONES
     */

    public function nombre() {
        return $this->roles[$this->rol_id];
    }

}

This is my users model

class Usuarios extends Authenticatable {

    use Notifiable;

    const STATUS_ACTIVO = 1;
    const STATUS_INACTIVO = 0;

    protected $table = 'usuarios';
    protected $primaryKey = 'usuario_id';

    /*
     * RELACIONES
     */

    public function _rol() {
         return $this->hasOne(Roles::class, 'rol', 'rol_id');
    }

    public function _perfil() {
        return $this->hasOne(Perfiles::class, "usuario", "usuario_id");
    }

    /*
     * FORMATEADORES
     */
}

But i got this error

Base table or view not found: 1146

When i try to do this

<?= Auth::user()->_rol->nombre()  ?>



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire