Extension Install
This commit is contained in:
110
extensions/Others/NavbarManager/Models/NavbarLink.php
Normal file
110
extensions/Others/NavbarManager/Models/NavbarLink.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?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;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user