<?php



namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

/**
 * Admin
 * @mixin Builder
 * @method static Builder|static where($column, $operator = null, $value = null, $boolean = 'and')
 */
class Role extends Authenticatable
{
    use HasApiTokens, Notifiable;
    protected $table = 'roles';
    protected $hidden = ['created_at', 'updated_at'];
    protected $fillable = ['name', 'display_name', 'description' ];
    protected $appends = ['menus_ids', 'menus_uris'];

    protected function getCreatedAtAttribute($value)
    {
        return \Carbon\Carbon::parse($value)->setTimezone('Asia/Shanghai')->format('Y-m-d H:i:s');
    }

    protected function getUpdatedAtAttribute($value)
    {
        return \Carbon\Carbon::parse($value)->setTimezone('Asia/Shanghai')->format('Y-m-d H:i:s');
    }

    public function users()
    {
        return $this->belongsToMany(Admin::class, 'role_user', 'role_id', 'user_id');
    }

     public function menus()
    {
        return $this->belongsToMany(Menu::class, 'role_menu', 'role_id', 'menu_id');
    }

    public function getMenusIdsAttribute()
    {
        return $this->menus->pluck('id')->toArray();
    }

    public function getMenusUrisAttribute()
    {
        return $this->menus->pluck('uri')->toArray();
    }
}