CategoryLabel.php 2.4 KB
<?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);
    }
}