CategoryLabel.php
2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
* 模型层:标签 模型类
+-----------------------------------------------------------------------------------------------------------------------
*
* PHP version 7
*
* @category App\Models\Category
* @package App\Models\Category
* @author Richer <yangzi1028@163.com>
* @date 2023年4月21日17:26:21
* @copyright 2014-2023 Richer (http://www.Richer.com/)
* @license http://www.Richer.com/ License
* @link http://www.Richer.com/
*/
namespace App\Models\Category;
use App\Models\BaseModel;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Arr;
/**
* Class CategoryLabel
* @property mixed name
* @property mixed icon
* @property mixed body
* @property mixed description
* @package App\Models
*/
class CategoryLabel extends BaseModel
{
// 指定数据库表
const TABLE = 'category_labels';
protected $table = self::TABLE ;
// 指定对象显示名称:方便系统统一查询和做其他处理
const OBJ_NAME = 'label';
const OBJ_NAME_ZH = '标签';
const CAN_MULTIPLE_OPTIONS = [
0 => '否',
1 => '是',
];
/**
* 访问器被附加到模型数组的形式。
*
* @var array
*/
protected $appends = ['can_multiple_show'];
/**
* @return BelongsTo
*/
public function category(): BelongsTo
{
return $this->belongsTo(Category::class);
}
/**
* @return HasMany
*/
public function items(): HasMany
{
return $this->hasMany(CategoryLabelItem::class, 'label_id');
}
/**
* @param $value
*/
public function setIconAttribute($value)
{
// 需要判断前端传来的路径是否是全路径,如果是全路径需要截取为相对路径
$this->attributes['icon'] = substr_file_path($value);
}
/**
* @param string $value 值
*
* @return array
* @throws GuzzleException
*/
public function getIconAttribute($value)
{
return splice_file_path($value);
}
/**
* 获取状态的中文显示
*
* @return mixed
*/
public function getCanMultipleShowAttribute()
{
return Arr::get(self::CAN_MULTIPLE_OPTIONS, $this->can_multiple ? : 0);
}
}