55 lines
1.4 KiB
PHP
Executable File
55 lines
1.4 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use App\Models\Tag;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class Categories extends Model
|
|
{
|
|
protected $guarded = [];
|
|
public $timestamps = false;
|
|
protected $casts = [
|
|
'aBanner' => 'array',
|
|
];
|
|
|
|
protected $touches = ['oArticles'];
|
|
|
|
public function aArticles(): HasMany {
|
|
return $this->hasMany(Articles::class, 'iCategoryId', 'id');
|
|
}
|
|
|
|
public function oArticles(): HasMany {
|
|
return $this->hasMany(Articles::class, 'iCategoryId', 'id');
|
|
}
|
|
|
|
public function aChildren()
|
|
{
|
|
return $this->hasMany(Categories::class, 'iParentId', 'id')->orderBy('iSort', 'desc');
|
|
}
|
|
|
|
public function oParent()
|
|
{
|
|
return $this->belongsTo(Categories::class, 'iParentId', 'id');
|
|
}
|
|
|
|
protected static function booted()
|
|
{
|
|
static::saved(fn() => Cache::forget('global_nav_tree'));
|
|
static::deleted(fn() => Cache::forget('global_nav_tree')); // 删除时也要清,这才叫严谨!
|
|
}
|
|
|
|
public static function getGlobalNavTree()
|
|
{
|
|
return self::where('iParentId', 0)
|
|
->where('iStatus', 1)
|
|
->with(['aChildren' => fn($q) => $q->where('iStatus', 1)->orderBy('iSort', 'desc')])
|
|
->orderBy('iSort', 'desc')
|
|
->get();
|
|
}
|
|
|
|
}
|