84 lines
1.9 KiB
PHP
84 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Encore\Admin\Traits\AdminBuilder;
|
|
use Encore\Admin\Traits\ModelTree;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
|
|
class Category extends Model
|
|
{
|
|
|
|
use AdminBuilder, ModelTree;
|
|
|
|
public const TYPE_SHOW = 'show';
|
|
public const TYPE_ARTICLE = 'article';
|
|
public const TYPE_ADVERT = 'advert';
|
|
public const TYPES = [
|
|
self::TYPE_ARTICLE => '文章列表',
|
|
self::TYPE_SHOW => '文章详情',
|
|
self::TYPE_ADVERT => '图片',
|
|
];
|
|
|
|
public function getLinkAttribute()
|
|
{
|
|
return route('category.show', $this);
|
|
}
|
|
|
|
/**
|
|
* 关联的数据
|
|
* @return [type] [description]
|
|
*/
|
|
public function relations()
|
|
{
|
|
switch ($this->type) {
|
|
case self::TYPE_SHOW:
|
|
return $this->hasOne(Article::class)->where('id', $this->article_id);
|
|
break;
|
|
case self::TYPE_ARTICLE:
|
|
return $this->belongsToMany(Article::class);
|
|
break;
|
|
case self::TYPE_ADVERT:
|
|
return $this->hasMany(Advert::class);
|
|
break;
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public function childrens()
|
|
{
|
|
return $this->hasMany(self::class, 'parent_id');
|
|
}
|
|
|
|
public function parent()
|
|
{
|
|
return $this->hasOne(self::class, 'id', 'parent_id');
|
|
}
|
|
|
|
public function article()
|
|
{
|
|
return $this->belongsTo(Article::class);
|
|
}
|
|
|
|
/**
|
|
* Notes: 获取当前分类及子分类ID
|
|
* @Author: <C.Jason>
|
|
* @Date : 2020/4/6 3:12 下午
|
|
* @return array
|
|
*/
|
|
public function getAllChildrenId()
|
|
{
|
|
$ids = array_keys($this->buildSelectOptions([], $this->id));
|
|
array_unshift($ids, $this->id);
|
|
|
|
return $ids;
|
|
}
|
|
|
|
public function articles(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Article::class);
|
|
}
|
|
|
|
}
|