<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Validator;

class Role extends GenericModel
{
    use HasFactory;

    protected $table = 'roles';
    protected $primaryKey = 'id';
    protected $label = 'Roles';
    protected $sub_label = 'Roles';
    protected $fields = [
        'nom'=>'nom',
        'updated_at'=>'Updated at',
    ];

    protected $orderBys = ['nom'=>'asc'];
    protected $actions = ['edit','add','delete'];
    protected $view_form = 'roles.add';
    protected $formRequireds = ['nom'];
    protected $datetimes = ['updated_at'];
    protected $hidden = ['id'];
    protected $fields_hiddens = ['id'];


    public function menus(){
        return $this->belongsToMany(Menu::class);
    }


    public static function validate($request){
        $rules = [
            'nom' => 'required|unique:roles,nom,'.$request->get('_id'),
        ];
        $messages = [
            'nom.required' => "nom is required.",
            'nom.unique' => "This nom already exists.",
        ];
        return Validator::make($request->input(),$rules,$messages);
    }
}
