131 lines
3.3 KiB
PHP
131 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\Traits\HasCovers;
|
|
use Encore\Admin\Traits\AdminBuilder;
|
|
use Encore\Admin\Traits\ModelTree;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
|
|
|
class Category extends Model
|
|
{
|
|
|
|
use AdminBuilder, ModelTree, HasCovers;
|
|
|
|
public const TYPE_SHOW = 'show';
|
|
public const TYPE_ARTICLE = 'article';
|
|
public const TYPE_ADVERT = 'advert';
|
|
public const TYPE_VIDEO = 'video';
|
|
public const TYPE_PHOTO = 'photo';
|
|
public const TYPE_LINK = 'link';
|
|
public const TYPES = [
|
|
self::TYPE_ARTICLE => '文章列表',
|
|
self::TYPE_SHOW => '文章详情',
|
|
self::TYPE_ADVERT => '图片列表',
|
|
self::TYPE_PHOTO => '图集',
|
|
self::TYPE_VIDEO => '视频列表',
|
|
self::TYPE_LINK => '跳转链接',
|
|
];
|
|
|
|
public $cover_field = 'cover';
|
|
|
|
public function getLogoUrlAttribute(): string
|
|
{
|
|
return $this->parseImageUrl($this->logo);
|
|
}
|
|
|
|
public function getLinkAttribute(): string
|
|
{
|
|
return route('category.show', $this);
|
|
}
|
|
|
|
/**
|
|
* Notes: description
|
|
*
|
|
* @Author: 玄尘
|
|
* @Date : 2021/10/14 11:53
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo|\Illuminate\Database\Eloquent\Relations\HasMany|null
|
|
*/
|
|
public function relations()
|
|
{
|
|
switch ($this->type) {
|
|
case self::TYPE_SHOW:
|
|
return $this->belongsTo(Article::class);
|
|
// return $this->hasOne(Article::class);
|
|
break;
|
|
case self::TYPE_ARTICLE:
|
|
// return $this->belongsToMany(Article::class);
|
|
return $this->hasMany(Article::class);
|
|
break;
|
|
case self::TYPE_ADVERT:
|
|
case self::TYPE_PHOTO:
|
|
return $this->hasMany(Advert::class);
|
|
break;
|
|
case self::TYPE_VIDEO:
|
|
return $this->hasMany(Video::class);
|
|
break;
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public function childrens(): HasMany
|
|
{
|
|
return $this->hasMany(self::class, 'parent_id');
|
|
}
|
|
|
|
public function parent(): HasOne
|
|
{
|
|
return $this->hasOne(self::class, 'id', 'parent_id');
|
|
}
|
|
|
|
public function article(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Article::class);
|
|
}
|
|
|
|
/**
|
|
* Notes: 获取当前分类及子分类ID
|
|
*
|
|
* @Author: <C.Jason>
|
|
* @Date : 2020/4/6 3:12 下午
|
|
* @return array
|
|
*/
|
|
public function getAllChildrenId(): array
|
|
{
|
|
$ids = array_keys($this->buildSelectOptions([], $this->id));
|
|
|
|
if ($ids) {
|
|
array_unshift($ids, $this->id);
|
|
|
|
return $ids;
|
|
}
|
|
|
|
return [$this->id];
|
|
}
|
|
|
|
// public function articles(): BelongsToMany
|
|
// {
|
|
// return $this->belongsToMany(Article::class);
|
|
// }
|
|
|
|
public function articles(): HasMany
|
|
{
|
|
return $this->hasMany(Article::class);
|
|
}
|
|
|
|
/**
|
|
* Notes: 格式化description
|
|
*
|
|
* @Author: 玄尘
|
|
* @Date : 2021/10/8 15:24
|
|
*/
|
|
public function getDescriptionHtmlAttribute(): string
|
|
{
|
|
return str_replace("\n", "</br>", $this->description);
|
|
}
|
|
|
|
}
|