94 lines
2.3 KiB
PHP
94 lines
2.3 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace plugin\article\app\model;
|
||
|
|
|
||
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||
|
|
|
||
|
|
class Content extends BaseModel
|
||
|
|
{
|
||
|
|
protected $table = 'article_content';
|
||
|
|
|
||
|
|
protected $fillable = [
|
||
|
|
'id',
|
||
|
|
'title',
|
||
|
|
'content_type',
|
||
|
|
'slug',
|
||
|
|
'category_id',
|
||
|
|
'channel',
|
||
|
|
'visibility',
|
||
|
|
'author',
|
||
|
|
'source',
|
||
|
|
'cover',
|
||
|
|
'seo_keywords',
|
||
|
|
'seo_title',
|
||
|
|
'seo_description',
|
||
|
|
'summary',
|
||
|
|
'content',
|
||
|
|
'status',
|
||
|
|
'is_top',
|
||
|
|
'is_recommend',
|
||
|
|
'allow_comment',
|
||
|
|
'sort',
|
||
|
|
'view_count',
|
||
|
|
'like_count',
|
||
|
|
'comment_count',
|
||
|
|
'favorite_count',
|
||
|
|
'published_at',
|
||
|
|
'offline_at',
|
||
|
|
'created_by',
|
||
|
|
'updated_by',
|
||
|
|
'created_at',
|
||
|
|
'updated_at',
|
||
|
|
'deleted_at',
|
||
|
|
];
|
||
|
|
|
||
|
|
protected $casts = [
|
||
|
|
'category_id' => 'integer',
|
||
|
|
'visibility' => 'integer',
|
||
|
|
'status' => 'integer',
|
||
|
|
'is_top' => 'integer',
|
||
|
|
'is_recommend' => 'integer',
|
||
|
|
'allow_comment' => 'integer',
|
||
|
|
'sort' => 'integer',
|
||
|
|
'view_count' => 'integer',
|
||
|
|
'like_count' => 'integer',
|
||
|
|
'comment_count' => 'integer',
|
||
|
|
'favorite_count' => 'integer',
|
||
|
|
'published_at' => 'integer',
|
||
|
|
'offline_at' => 'integer',
|
||
|
|
'created_at' => 'integer',
|
||
|
|
'updated_at' => 'integer',
|
||
|
|
'deleted_at' => 'integer',
|
||
|
|
];
|
||
|
|
|
||
|
|
protected $appends = ['tag_names', 'tag_ids', 'category_name', 'created_date', 'updated_date', 'created_name', 'updated_name'];
|
||
|
|
|
||
|
|
public function category(): BelongsTo
|
||
|
|
{
|
||
|
|
return $this->belongsTo(Category::class, 'category_id', 'id');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function tags(): BelongsToMany
|
||
|
|
{
|
||
|
|
return $this->belongsToMany(Tag::class, 'article_content_tag', 'content_id', 'tag_id');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getCategoryNameAttribute(): string
|
||
|
|
{
|
||
|
|
return $this->category?->name ?? '';
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getTagNamesAttribute(): string
|
||
|
|
{
|
||
|
|
return $this->tags->pluck('name')->implode('、');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function getTagIdsAttribute(): array
|
||
|
|
{
|
||
|
|
return $this->tags->pluck('id')->map(static fn($id) => is_numeric($id) ? (int)$id : $id)->values()->all();
|
||
|
|
}
|
||
|
|
}
|