fn_b/app/Models/Categories.php
2026-02-12 12:36:53 +08:00

50 lines
1.3 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;
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 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();
}
}