Files
ProxyPanel/app/Models/Goods.php
BrettonYe 4cfb0b4650 💪🏼 Improve Code
2024-06-27 22:18:26 +08:00

58 lines
1.3 KiB
PHP

<?php
namespace App\Models;
use App\Casts\data_rate;
use App\Casts\money;
use App\Utils\Helpers;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* 商品
*/
class Goods extends Model
{
use SoftDeletes;
protected $table = 'goods';
protected $casts = ['price' => money::class, 'renew' => money::class, 'speed_limit' => data_rate::class, 'deleted_at' => 'datetime'];
protected $guarded = [];
public function orders(): HasMany
{
return $this->hasMany(Order::class);
}
public function scopeType(Builder $query, int $type): Builder
{
return $query->whereType($type)->whereStatus(1)->orderByDesc('sort');
}
protected function priceTag(): Attribute
{
return Attribute::make(
get: fn () => Helpers::getPriceTag($this->price),
);
}
protected function renewTag(): Attribute
{
return Attribute::make(
get: fn () => Helpers::getPriceTag($this->renew),
);
}
protected function trafficLabel(): Attribute
{
return Attribute::make(
get: fn () => formatBytes($this->traffic, 'MiB'),
);
}
}