Files
xin-procurement/app/Models/ProductModel.php
T
2026-08-27 14:20:09 +08:00

101 lines
2.5 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Modules\SystemTool\Models\SysFileModel;
/**
* 商品档案模型(品名/规格包规/供应商/成本价;售价按客户等级上浮比例换算)
*/
class ProductModel extends Model
{
use SoftDeletes, HasFactory;
/** 状态:下架 */
public const int STATUS_OFF = 0;
/** 状态:上架 */
public const int STATUS_ON = 1;
protected $table = 'product';
protected $primaryKey = 'id';
protected $fillable = [
'category_id',
'supplier_id',
'market',
'name',
'spec',
'unit',
'price_unit',
'image_ids',
'content',
'sort',
'shelf_life',
'stock',
'status',
'cost_price',
'remark',
];
protected $casts = [
'category_id' => 'integer',
'supplier_id' => 'integer',
'shelf_life' => 'integer',
'stock' => 'integer',
'sort' => 'integer',
'status' => 'integer',
'cost_price' => 'decimal:2',
'created_at' => 'datetime:Y-m-d H:i:s',
'updated_at' => 'datetime:Y-m-d H:i:s',
];
protected $appends = ['images_arr'];
/**
* 成本价属商业敏感数据,默认不随 toArray 输出(防止泄漏到小程序/供应商端);
* 后台管理接口需在查询结果上调用 makeVisible('cost_price') 恢复。
*/
protected $hidden = ['cost_price'];
/**
* 封面图片
*/
public function imageIds(): Attribute
{
return Attribute::make(
get: fn ($value) => $value ? explode(',', $value) : [],
set: fn ($value) => is_array($value) ? implode(',', $value) : $value,
);
}
// 获取封面图片关联数据
public function getImagesArrAttribute(): array
{
if( $this->image_ids ) {
return SysFileModel::whereIn('id', $this->image_ids )->get()->toArray();
}
return [];
}
/**
* 所属分类
*/
public function category(): BelongsTo
{
return $this->belongsTo(ProductCategoryModel::class, 'category_id', 'id');
}
/**
* 供货供应商
*/
public function supplier(): BelongsTo
{
return $this->belongsTo(SupplierModel::class, 'supplier_id', 'id');
}
}