<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Zizaco\Entrust\Traits\EntrustUserTrait;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable{

    use EntrustUserTrait;
    use Notifiable;

    protected $table = 'usuario';

    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function getFullNameAttribute() {
        return ucfirst($this->nombres) . ' ' . ucfirst($this->apellido_paterno);
    }

    public function getRoleIdAttribute(){
        return $this->roles->first()->name;
    }

    public function roles(){
        return $this->belongsToMany('App\Model\Role');
    }
    public function idroles(){
        $Arr = array();

        foreach (json_decode(json_encode($this->roles), true) as $item){
            array_push($Arr,$item['pivot']['role_id']);
        }
        return $Arr;
    }

    public function getRoleAttribute(){
        $roles = $this->roles()->get();
        $rol = "";
        foreach($roles as $aux){
            $rol = $aux->name.", ".$rol;
        }
        $rol = rtrim($rol, ', ');
        return $rol;
    }
}