114 lines
3.3 KiB
PHP
Executable File
114 lines
3.3 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Spatie\ResponseCache\Facades\ResponseCache;
|
|
use App\Models\Tags;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Laravel\Scout\Searchable;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
|
|
class Articles extends Model
|
|
{
|
|
protected $guarded = [];
|
|
protected $appends = ['s_created_at_diff'];
|
|
|
|
use Searchable;
|
|
|
|
protected function sCreatedAtDiff(): Attribute
|
|
{
|
|
return Attribute::get(function () {
|
|
// 1. 别直接用 $this->created_at,防止它还没被实例化成 Carbon 对象
|
|
$oDate = \Carbon\Carbon::parse($this->created_at);
|
|
$oNow = \Carbon\Carbon::now();
|
|
|
|
// 2. 计算小时差(强制转整数)
|
|
$iHours = (int)$oDate->diffInHours($oNow);
|
|
|
|
if ($iHours < 1) {
|
|
// 不足 1 小时,看看是不是不足 1 分钟
|
|
$iMinutes = (int)$oDate->diffInMinutes($oNow);
|
|
return $iMinutes < 1 ? '刚刚' : $iMinutes . '分钟前';
|
|
}
|
|
|
|
if ($iHours < 24) {
|
|
return $iHours . '小时前';
|
|
}
|
|
|
|
// 3. 计算天数差(强制转整数)
|
|
$iDays = (int)$oDate->diffInDays($oNow);
|
|
if ($iDays < 30) {
|
|
return $iDays . '天前';
|
|
}
|
|
|
|
// 超过一个月直接显示日期
|
|
return $oDate->format('Y-m-d');
|
|
});
|
|
}
|
|
|
|
public function shouldBeSearchable(): bool
|
|
{
|
|
return (int)$this->iStatus === 1;
|
|
}
|
|
|
|
public function searchableAs(): string
|
|
{
|
|
return 'articles';
|
|
}
|
|
|
|
public function toSearchableArray(): array
|
|
{
|
|
// 药水哥原则:字段要精准,类型要死磕
|
|
return [
|
|
'id' => (int) $this->id,
|
|
'iCategoryId' => (int) $this->iCategoryId,
|
|
'sCategoryName' => (string) ($this->oCategory->sName ?? 'none'),
|
|
'sCategorySlug' => (string) ($this->oCategory->sSlug ?? 'none'),
|
|
'sTitle' => (string) $this->sTitle,
|
|
'sTitleSub' => (string) $this->sTitleSub,
|
|
'sSlug' => (string) $this->sSlug,
|
|
'sContent' => (string) mb_substr(strip_tags($this->sContent), 0, 500),
|
|
'iStatus' => (int) $this->iStatus,
|
|
'iIsTop' => (int) $this->iIsTop,
|
|
'updated_at' => (int) $this->updated_at->timestamp,
|
|
];
|
|
}
|
|
|
|
protected static function booted()
|
|
{
|
|
static::saved(function () {
|
|
ResponseCache::forget('/'); // 要改成具体文章
|
|
});
|
|
|
|
static::deleted(function () {
|
|
ResponseCache::forget('/'); // 要改成具体文章
|
|
});
|
|
}
|
|
|
|
|
|
|
|
public function oTags(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(
|
|
Tags::class,
|
|
'article_tag',
|
|
'iArticleId',
|
|
'iTagId'
|
|
);
|
|
}
|
|
|
|
public function oCategory(): BelongsTo {
|
|
return $this->belongsTo(Categories::class, 'iCategoryId', 'id');
|
|
}
|
|
|
|
public function oUser()
|
|
{
|
|
return $this->belongsTo(User::class, 'iUserId', 'id');
|
|
}
|
|
|
|
}
|
|
|