Files
my-projects-website/app/application/app/Models/Role.php
T
kor-elf b7d0a2453e Moved from "app/src" to "app/application".
Otherwise phpstorm doesn't understand the paths correctly. He thinks that this is not a complete application, but a package. And when creating a class, the namespace indicates “app” with a small letter, but should be “App”.
2024-04-11 19:52:37 +05:00

76 lines
1.9 KiB
PHP

<?php declare(strict_types=1);
namespace App\Models;
use App\Enums\SystemRole as SystemRoleEnum;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\hasMany;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\SoftDeletes;
final class Role extends Model
{
use HasFactory, SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'code',
];
public function scopeLatest(Builder $query): Builder
{
return $query->orderBy('id', 'desc');
}
public function scopeAlphavit(Builder $query): Builder
{
return $query->orderBy('name', 'asc');
}
protected function nameWithMorph(): Attribute
{
return Attribute::make(
get: function () {
$name = $this->name;
if ($this->morph_type) {
$name .= ' (' . __('admin-sections.Projects') . ': ' . $this->morph?->name . ')';
}
return $name;
},
)->shouldCache();
}
public function morph(): MorphTo
{
return $this->morphTo('morph');
}
public function permissions(): hasMany
{
return $this->hasMany(RolePermission::class, 'role_id', 'id');
}
protected function isRemove(): Attribute
{
return Attribute::make(
get: fn ($dontRemove) => ( SystemRoleEnum::tryFrom($this->code) === null ),
)->shouldCache();
}
protected function isAdmin(): Attribute
{
return Attribute::make(
get: fn () => ( \is_null($this->morphable_type) && \is_null($this->morphable_id) && $this->code === SystemRoleEnum::Admin->value ),
)->shouldCache();
}
}