<?php

namespace App\Models\SM;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
    use HasFactory;

    protected $fillable = ['name', 'app_code', 'desc', 'created_by'];
    protected $casts = ['created_at' => 'date:d/m/Y'];

    public function fncPermissions()
    {
        return $this->belongsToMany(FnctionPermission::class, RolePermission::class, 'role_id', 'permission_id');
    }

    public function fnctions()
    {
        return $this->belongsToMany(Fnction::class, RoleFnction::class, 'role_id', 'fnction_id');
    }

    public static function boot()
    {
        Parent::boot();
        static::creating(function ($model) {
            $model->created_by = auth()->id();
        });
    }
}
