<?php

namespace App\Models;

use Spatie\Permission\Models\Role as OriginalRole;

class Role extends OriginalRole
{
    protected static function booted()
    {
        static::creating(function ($model) {
            if(auth()->check()){
                $model->created_by = auth()->id();
                $model->name = date("ym").$model->created_by.'-'.$model->name;
            }
            
        });

        static::addGlobalScope('createdBy', function ($builder) {
            if(auth()->check()){
                $builder->where('created_by', auth()->id());   
                $builder->orWhere('created_by', null);
            }
        });
    }
}
