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

90 lines
2.2 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\Relations\HasMany;
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',
'name',
'spec',
'unit',
'images',
'content',
'sort',
'shelf_life',
'stock',
'status',
'remark',
];
protected $casts = [
'category_id' => 'integer',
'supplier_id' => 'integer',
'shelf_life' => 'integer',
'stock' => 'integer',
'sort' => 'integer',
'status' => 'integer',
];
public function images(): Attribute
{
return Attribute::make(
get: function($value){
if (empty($value)) {
return collect();
}
$ids = explode(',', $value);
return SysFileModel::whereIn('id', $ids)->get();
},
set: fn ($value) => is_array($value) ? implode(',', $value) : $value,
);
}
/**
* 所属分类
*/
public function category(): BelongsTo
{
return $this->belongsTo(ProductCategoryModel::class, 'category_id', 'id');
}
/**
* 供货供应商
*/
public function supplier(): BelongsTo
{
return $this->belongsTo(SupplierModel::class, 'supplier_id', 'id');
}
/**
* 多等级价格(同一商品按客户等级定价)
*/
public function prices(): HasMany
{
return $this->hasMany(ProductPriceModel::class, 'product_id', 'id');
}
}