'会员商品',
'pick' => '提货商品',
];
public function link()
{
return route('goods.show', $this);
}
protected function getTypeTextAttribute()
{
return $this->typeArray[$this->type] ?? '未知';
}
public function category()
{
return $this->belongsTo(Category::class)->withDefault();
}
public function params()
{
return $this->hasMany(GoodsParams::class, 'goods_id', 'id')->where('status', 1);
}
public function getTitle()
{
return $this->title;
}
public function getPrice()
{
return $this->price;
}
public function getScore()
{
return $this->score;
}
public function getStock()
{
return $this->stock;
}
public function getSellerPrice()
{
return $this->cost;
}
public function deductStock($stock)
{
$this->decrement('stock', $stock);
}
public function addStock($stock)
{
$this->increment('stock', $stock);
}
public function addSold($sold)
{
$this->increment('sold', $sold);
}
protected function getMinPriceAttribute()
{
return number_format($this->params()->min('price'), 2);
}
protected function getDefMinPriceAttribute()
{
if (Auth::user() && Auth::user()->identity_id > 0) {
return number_format($this->params()->min('price'), 2);
} else {
return number_format($this->params()->min('original'), 2);
}
}
protected function getMinOriginalAttribute()
{
return number_format($this->params()->min('original'), 2);
}
protected function getAllStockAttribute()
{
return $this->params()->sum('stock') ?? 0;
}
protected function getScoreRangeAttribute()
{
$params = $this->params()->orderBy('score', 'desc')->distinct()->pluck('score');
$params_size = count($params);
if ($params_size == 1) {
return number_format($params[0], 2);
} elseif ($params_size > 1) {
if ($params[$params_size - 1] > 0) {
return number_format($params[0], 2) . '~' . number_format($params[$params_size - 1], 2);
} else {
return number_format($params[0], 2);
}
} else {
return 0.00;
}
}
protected function getStatusTextAttribute()
{
switch ($this->status) {
case -1:
return "待审核";
break;
case 0:
return "下架";
break;
case 1:
return "正常";
break;
default:
return "未知";
break;
}
}
public function getIsFavoriteAttribute()
{
$user = Auth::user();
if (!$user) {
return false;
}
if ($this->isFavoritedBy($user)) {
return true;
} else {
return false;
}
}
public function getCoverPathAttribute()
{
if ($this->cover) {
return env('APP_URL') . '/storage/' . $this->cover;
} else {
return '';
}
}
}