二次改版
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Models\Traits\HasCovers;
|
||||
use Encore\Admin\Traits\AdminBuilder;
|
||||
use Encore\Admin\Traits\ModelTree;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
@@ -9,33 +10,50 @@ use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
class Category extends Model
|
||||
{
|
||||
|
||||
use AdminBuilder, ModelTree;
|
||||
use AdminBuilder, ModelTree, HasCovers;
|
||||
|
||||
public const TYPE_SHOW = 'show';
|
||||
public const TYPE_ARTICLE = 'article';
|
||||
public const TYPE_ADVERT = 'advert';
|
||||
const TYPE_LINK = 'link';
|
||||
const TYPE_ARTICLE = 'article';
|
||||
const TYPE_ADVERT = 'advert';
|
||||
const TYPE_PERSON = 'person';
|
||||
const TYPE_LEADER = 'leader';
|
||||
|
||||
public const TYPES = [
|
||||
const TYPES = [
|
||||
self::TYPE_ARTICLE => '文章列表',
|
||||
self::TYPE_SHOW => '文章详情',
|
||||
self::TYPE_LINK => '外链',
|
||||
self::TYPE_ADVERT => '图片',
|
||||
self::TYPE_PERSON => '领导',
|
||||
self::TYPE_LEADER => '人才梯队',
|
||||
];
|
||||
|
||||
public function getLinkAttribute()
|
||||
/**
|
||||
* Notes: 跳转链接
|
||||
*
|
||||
* @Author: 玄尘
|
||||
* @Date: 2022/4/28 13:53
|
||||
* @return string
|
||||
*/
|
||||
public function getLinkAttribute(): string
|
||||
{
|
||||
return route('category.show', $this);
|
||||
switch ($this->type) {
|
||||
case self::TYPE_LEADER:
|
||||
return route('leader.index');
|
||||
case self::TYPE_PERSON:
|
||||
return route('staff.index');
|
||||
default:
|
||||
return route('category.show', $this);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 关联的数据
|
||||
* @return [type] [description]
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo|\Illuminate\Database\Eloquent\Relations\BelongsToMany|\Illuminate\Database\Eloquent\Relations\HasMany|null [type] [description]
|
||||
*/
|
||||
public function relations()
|
||||
{
|
||||
switch ($this->type) {
|
||||
case self::TYPE_SHOW:
|
||||
return $this->belongsTo(Article::class);
|
||||
break;
|
||||
case self::TYPE_ARTICLE:
|
||||
return $this->belongsToMany(Article::class);
|
||||
break;
|
||||
@@ -61,7 +79,7 @@ class Category extends Model
|
||||
{
|
||||
return $this->belongsTo(Article::class);
|
||||
}
|
||||
|
||||
|
||||
public function articles(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Article::class);
|
||||
@@ -69,6 +87,7 @@ class Category extends Model
|
||||
|
||||
/**
|
||||
* Notes: 获取当前分类及子分类ID
|
||||
*
|
||||
* @Author: <C.Jason>
|
||||
* @Date : 2020/4/6 3:12 下午
|
||||
* @return array
|
||||
@@ -81,16 +100,25 @@ class Category extends Model
|
||||
return $ids;
|
||||
}
|
||||
|
||||
//查找顶级分类
|
||||
public function getTop()
|
||||
/**
|
||||
* Notes: 获取顶级分类
|
||||
*
|
||||
* @Author: 玄尘
|
||||
* @Date: 2022/4/28 13:52
|
||||
* @param $category_id
|
||||
* @return void
|
||||
*/
|
||||
public function getTopCategory()
|
||||
{
|
||||
$parent = $this;
|
||||
$category = $this;
|
||||
|
||||
while ($parent->parent_id != 0) {
|
||||
$parent = $parent->parent;
|
||||
while ($category) {
|
||||
if ($category->parent) {
|
||||
$category = $category->parent;
|
||||
} else {
|
||||
return $category;
|
||||
}
|
||||
}
|
||||
|
||||
return $parent;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user