43 lines
1017 B
PHP
43 lines
1017 B
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\PriceType;
|
|
use Illuminate\Database\Eloquent\Attributes\Appends;
|
|
use Illuminate\Database\Eloquent\Attributes\Guarded;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
#[Guarded(['id'])]
|
|
#[Appends(['price_formatted', 'price_type_label'])]
|
|
class ProductPrice extends Model
|
|
{
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'price_type' => PriceType::class,
|
|
'price' => 'integer',
|
|
];
|
|
}
|
|
|
|
protected function priceTypeLabel(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => $this->price_type->label(),
|
|
);
|
|
}
|
|
|
|
protected function priceFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn () => 'Rp '.number_format($this->price, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
public function product(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Product::class);
|
|
}
|
|
}
|