master_template/app/Models/Menuitems.php

72 lines
1.8 KiB
PHP
Raw Normal View History

2024-06-10 12:21:58 +00:00
<?php
namespace App\Models;
use App\Models\User;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Traits\CreatedUpdatedBy;
class Menuitems extends Model
{
use HasFactory, CreatedUpdatedBy;
protected $primaryKey = 'menu_id';
public $timestamps = true;
protected $fillable = [
'parent_menu',
'menulocations_id',
'title',
'alias',
'type',
'ref',
'target',
'display_order',
'status',
'created_at',
'updated_at',
'createdby',
'updatedby',
];
protected $appends = ['status_name'];
protected function getStatusNameAttribute()
{
return $this->status == 1 ? '<span class="badge text-bg-success-soft"> Active </span>' : '<span class="badge text-bg-danger-soft">Inactive</span>';
}
protected function createdBy(): Attribute
{
return Attribute::make(
get: fn ($value) => User::find($value) ? User::find($value)->name : '',
);
}
protected function updatedBy(): Attribute
{
return Attribute::make(
get: fn ($value) => User::find($value) ? User::find($value)->name : '',
);
}
public function parent()
{
return $this->belongsTo(Menuitems::class, 'parent_menu')->where('status', 1);
}
public function children()
{
return $this->hasMany(Menuitems::class, 'parent_menu')->orderBy('display_order')->where('status', 1);
}
public function subchildren()
{
return $this->hasMany(Menuitems::class, 'parent_menu')
->where('status', 1)
->orderBy('display_order')
->with('children');
}
}