first change
This commit is contained in:
132
Modules/Menu/app/Models/Menu.php
Normal file
132
Modules/Menu/app/Models/Menu.php
Normal file
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Menu\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Modules\CCMS\Models\Blog;
|
||||
use Modules\CCMS\Models\Country;
|
||||
use Modules\CCMS\Models\Page;
|
||||
use Modules\CCMS\Models\Service;
|
||||
|
||||
class Menu extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected $fillable = [
|
||||
'menu_location_id',
|
||||
'title',
|
||||
'alias',
|
||||
'target',
|
||||
'image',
|
||||
'icon',
|
||||
'parent_id',
|
||||
'order',
|
||||
'type',
|
||||
'parameter',
|
||||
'status',
|
||||
'order',
|
||||
];
|
||||
|
||||
public $appends = ['location'];
|
||||
|
||||
public function parent()
|
||||
{
|
||||
return $this->belongsTo(Menu::class, 'parent_id');
|
||||
}
|
||||
|
||||
protected function urlParameter(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: function (?string $value, ?array $attributes) {
|
||||
$parameter = null;
|
||||
|
||||
switch ($attributes['type']) {
|
||||
case 'single-link':
|
||||
$parameter = $attributes['parameter'];
|
||||
break;
|
||||
|
||||
default:
|
||||
$model = config('constants.menu_type_options')[$attributes['type']] ?? null;
|
||||
if ($model) {
|
||||
$modelClass = "Modules\\CCMS\\Models\\$model";
|
||||
$parameter = optional($modelClass::find($attributes['parameter']))->slug;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return $parameter;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public function children()
|
||||
{
|
||||
return $this->hasMany(Menu::class, 'parent_id');
|
||||
}
|
||||
|
||||
public function location(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: function (mixed $value, array $attributes) {
|
||||
return config('constants.menu_location_options')[$attributes['menu_location_id']];
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public function hasSubMenu()
|
||||
{
|
||||
return $this->relationLoaded('children') ? $this->children->isNotEmpty() : $this->children()->exists();
|
||||
}
|
||||
|
||||
|
||||
protected function routeName(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: function (mixed $value, array $attributes) {
|
||||
|
||||
$route = null;
|
||||
|
||||
switch ($attributes['type']) {
|
||||
case 'pages':
|
||||
$route = route('page.load', Page::whereId($attributes['parameter'])->value('slug'));
|
||||
break;
|
||||
|
||||
case 'services':
|
||||
$route = route('service.single', Service::whereId($attributes['parameter'])->value('slug'));
|
||||
break;
|
||||
|
||||
case 'countries':
|
||||
$route = route('country.single', Country::whereId($attributes['parameter'])->value('slug'));
|
||||
break;
|
||||
|
||||
case 'blogs':
|
||||
$route = route('blog.single', Blog::whereId($attributes['parameter'])->value('slug'));
|
||||
break;
|
||||
|
||||
default:
|
||||
$route = ($attributes['parameter'] == '#')
|
||||
? 'javascript:void(0)'
|
||||
: (preg_match('/^https/', $attributes['parameter'])
|
||||
? $attributes['parameter']
|
||||
: config('app.url') . $attributes['parameter']);
|
||||
|
||||
}
|
||||
|
||||
return $route;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
protected function image(): Attribute
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn($value) => asset($value),
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user