Files
new_haai/app/Models/Article.php
2021-04-02 09:30:56 +08:00

61 lines
1.3 KiB
PHP

<?php
namespace App\Models;
use App\Models\Traits\BelongsToCategory;
use App\Models\Traits\HasOneCover;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
class Article extends Model
{
use HasOneCover;
const POSITION_A = 1;
const POSITION_B = 2;
const POSITION_C = 3;
const POSITIONS = [
self::POSITION_A => '院所新闻',
self::POSITION_B => '科学研究',
self::POSITION_C => '科技成果',
];
public function getLinkAttribute(): string
{
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);
});
}
}