53 lines
1.1 KiB
PHP
53 lines
1.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use EloquentFilter\Filterable;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* 文章分类
|
|
*/
|
|
class ArticleCategory extends Model
|
|
{
|
|
use Filterable;
|
|
|
|
protected $casts = [
|
|
'created_at' => 'datetime:Y-m-d H:i:s',
|
|
'updated_at' => 'datetime:Y-m-d H:i:s',
|
|
'is_enable' => 'boolean',
|
|
];
|
|
|
|
protected $fillable = ['icon', 'is_enable', 'level', 'name', 'parent_id', 'path', 'sort'];
|
|
|
|
protected function icon(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn($value) => $value ? (Str::startsWith($value, ['http://', 'https://']) ? $value : Storage::url($value)) : '',
|
|
);
|
|
}
|
|
|
|
public function parent()
|
|
{
|
|
return $this->belongsTo(static::class, 'parent_id');
|
|
}
|
|
|
|
public function children()
|
|
{
|
|
return $this->hasMany(static::class, 'parent_id');
|
|
}
|
|
|
|
public function scopeSort($q)
|
|
{
|
|
return $q->orderBy('sort', 'desc');
|
|
}
|
|
|
|
public function scopeShow($q)
|
|
{
|
|
return $q->where('is_enable', 1);
|
|
}
|
|
}
|