firstpush
This commit is contained in:
57
app/Models/Category.php
Normal file
57
app/Models/Category.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Encore\Admin\Traits\AdminBuilder;
|
||||
use Encore\Admin\Traits\ModelTree;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class Category extends Model
|
||||
{
|
||||
use AdminBuilder, ModelTree;
|
||||
|
||||
public const TYPES = [
|
||||
'article' => '文章列表',
|
||||
'show' => '文章详情',
|
||||
'advert' => '广告',
|
||||
'picture' => '图册',
|
||||
];
|
||||
|
||||
public const TYPE_SHOW = 'show';
|
||||
public const TYPE_ARTICLE = 'article';
|
||||
public const TYPE_ADVERT = 'advert';
|
||||
public const TYPE_PICTURE = 'picture';
|
||||
|
||||
/**
|
||||
* 关联的数据
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function relations()
|
||||
{
|
||||
switch ($this->type) {
|
||||
case self::TYPE_SHOW:
|
||||
return $this->hasOne(Article::class,'id','article_id');
|
||||
break;
|
||||
case self::TYPE_ARTICLE:
|
||||
return $this->hasMany(Article::class);
|
||||
break;
|
||||
case self::TYPE_ADVERT:
|
||||
return $this->hasMany(Advert::class);
|
||||
break;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public function parent(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(__CLASS__)->withDefault(['name' => '顶级分类']);
|
||||
}
|
||||
|
||||
public function children(): HasMany
|
||||
{
|
||||
return $this->hasMany(__CLASS__, 'parent_id');
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user