6
0
Fork 0
jiqu-library-server/app/Models/ProductSku.php

80 lines
1.6 KiB
PHP

<?php
namespace App\Models;
use App\Casts\JsonArray;
use App\Casts\Price;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Model;
class ProductSku extends Model
{
use Filterable;
use HasDateTimeFormatter;
/**
* @var array
*/
protected $casts = [
'images' => JsonArray::class,
'sell_price' => Price::class,
'market_price' => Price::class,
'cost_price' => Price::class,
'vip_price' => Price::class,
'attrs' => JsonArray::class,
'specs' => 'json',
'release_at' => 'datetime',
];
/**
* @var array
*/
protected $fillable = [
'spu_id',
'name',
'subtitle',
'category_id',
'codcovere',
'images',
'sell_price',
'market_price',
'cost_price',
'vip_price',
'media',
'weight',
'attrs',
'specs',
'stock',
'sales',
'release_at',
];
/**
* 仅查询已上架的商品
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeIsRelease($query)
{
return $query->whereNotNull('release_at');
}
/**
* 此商品所属的分类
*/
public function category()
{
return $this->belongsTo(ProductCategory::class, 'category_id');
}
/**
* 此商品所属的分区
*/
public function parts()
{
return $this->belongsToMany(ProductPart::class, ProductPartSku::class, 'sku_id', 'part_id');
}
}