50 lines
1.4 KiB
PHP
50 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Support\Str;
|
|
use EloquentFilter\Filterable;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
/**
|
|
* 文章
|
|
*/
|
|
class Article extends Model
|
|
{
|
|
use Filterable;
|
|
|
|
protected $fillable = ['author', 'category_id', 'category_path', 'content', 'cover', 'is_enable', 'is_recommend', 'published_at', 'sort', 'sub_title', 'title'];
|
|
|
|
protected $casts = [
|
|
'created_at' => 'datetime:Y-m-d H:i:s',
|
|
'updated_at' => 'datetime:Y-m-d H:i:s',
|
|
'published_at' => 'datetime:Y-m-d H:i:s',
|
|
'is_enable' => 'boolean',
|
|
'is_recommend' => 'boolean',
|
|
];
|
|
|
|
protected function cover(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn($value) => $value ? (Str::startsWith($value, ['http://', 'https://']) ? $value : Storage::url($value)) : '',
|
|
);
|
|
}
|
|
|
|
public function category()
|
|
{
|
|
return $this->belongsTo(ArticleCategory::class, 'category_id');
|
|
}
|
|
|
|
public function scopeSort($q)
|
|
{
|
|
return $q->orderBy('sort', 'desc')->orderBy('published_at', 'desc');
|
|
}
|
|
|
|
public function scopeShow($q)
|
|
{
|
|
return $q->where('is_enable', 1)->where(fn ($q1) => $q1->whereNull('published_at')->orWhere('published_at', '<=', now()));
|
|
}
|
|
}
|