Files
Virtnet.bond/extensions/Others/NavbarManager/Models/NavbarLink.php
2025-12-03 14:23:33 +01:00

111 lines
2.9 KiB
PHP

<?php
namespace Paymenter\Extensions\Others\NavbarManager\Models;
use Illuminate\Database\Eloquent\Model;
class NavbarLink extends Model
{
protected $table = 'navbar_links';
protected $fillable = [
'name',
'type',
'link_type',
'route_name',
'route_params',
'custom_path',
'custom_url',
'product_id',
'category_id',
'icon',
'condition',
'sort_order',
'is_active',
'parent_id',
];
protected $casts = [
'is_active' => 'boolean',
'route_params' => 'json',
];
public function parent()
{
return $this->belongsTo(NavbarLink::class, 'parent_id');
}
public function children()
{
return $this->hasMany(NavbarLink::class, 'parent_id')->orderBy('sort_order');
}
public function product()
{
return $this->belongsTo(\App\Models\Product::class);
}
public function category()
{
return $this->belongsTo(\App\Models\Category::class);
}
public function scopeActive($query)
{
return $query->where('is_active', true);
}
public function scopeTopLevel($query)
{
return $query->whereNull('parent_id');
}
public function scopeByType($query, $type)
{
return $query->where('type', $type);
}
protected static function booted()
{
static::saving(function ($link) {
// Clear unused fields based on link_type
switch ($link->link_type) {
case 'route':
$link->custom_path = null;
$link->custom_url = null;
$link->product_id = null;
$link->category_id = null;
break;
case 'path':
$link->route_name = null;
$link->route_params = null;
$link->custom_url = null;
$link->product_id = null;
$link->category_id = null;
break;
case 'url':
$link->route_name = null;
$link->route_params = null;
$link->custom_path = null;
$link->product_id = null;
$link->category_id = null;
break;
case 'product':
$link->route_name = null;
$link->route_params = null;
$link->custom_path = null;
$link->custom_url = null;
$link->category_id = null;
break;
case 'category':
$link->route_name = null;
$link->route_params = null;
$link->custom_path = null;
$link->custom_url = null;
$link->product_id = null;
break;
}
});
}
}