75 lines
1.4 KiB
PHP
75 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Traits\HasCovers;
|
|
use App\Models\Traits\HasSort;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
|
|
class Article extends Model
|
|
{
|
|
|
|
use HasCovers,HasSort;
|
|
|
|
/**
|
|
* 应进行类型转换的属性
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $casts = [
|
|
'subjoin' => 'array',
|
|
'pictures' => 'array',
|
|
];
|
|
|
|
public function getLinkAttribute()
|
|
{
|
|
return route('article.show', $this);
|
|
}
|
|
|
|
/**
|
|
* Notes: 关联分类
|
|
*
|
|
* @Author: 玄尘
|
|
* @Date : 2021/4/2 9:11
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
*/
|
|
public function categories(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Category::class)
|
|
->using(ArticleCategory::class);
|
|
}
|
|
|
|
/**
|
|
* Notes: description
|
|
*
|
|
* @Author: 玄尘
|
|
* @Date : 2021/4/2 9:17
|
|
* @param $query
|
|
* @param $ids
|
|
* @return mixed
|
|
*/
|
|
public function scopeByCategory($query, $ids)
|
|
{
|
|
if (! is_array($ids)) {
|
|
$ids = [$ids];
|
|
}
|
|
|
|
return $query->whereHas('categories', function ($q) use ($ids) {
|
|
$q->whereIN('id', $ids);
|
|
});
|
|
}
|
|
|
|
|
|
|
|
public function getDateD()
|
|
{
|
|
return $this->created_at->format('d');
|
|
}
|
|
|
|
public function getDateYM()
|
|
{
|
|
return $this->created_at->format('Y-m');
|
|
}
|
|
|
|
}
|