65 lines
1.6 KiB
PHP
65 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\PriceType;
|
|
use App\Models\Concerns\InteractsWithActivityLog;
|
|
use Illuminate\Database\Eloquent\Attributes\Appends;
|
|
use Illuminate\Database\Eloquent\Attributes\Guarded;
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Spatie\Activitylog\Support\LogOptions;
|
|
|
|
#[Guarded(['id'])]
|
|
#[Appends(['price_formatted', 'price_input'])]
|
|
class ProductPrice extends Model
|
|
{
|
|
// 1. Use Trait
|
|
use HasFactory, InteractsWithActivityLog;
|
|
|
|
// 2. Casting
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'price' => 'integer',
|
|
'type' => PriceType::class,
|
|
];
|
|
}
|
|
|
|
public function getActivitylogOptions(): LogOptions
|
|
{
|
|
return LogOptions::defaults()
|
|
->logUnguarded()
|
|
->logOnlyDirty()
|
|
->dontLogEmptyChanges()
|
|
->logExcept([
|
|
'id',
|
|
'variant_id',
|
|
'deleted_at',
|
|
]);
|
|
}
|
|
|
|
// 3. Attribute
|
|
public function priceFormatted(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn() => 'Rp ' . number_format($this->price, 0, ',', '.'),
|
|
);
|
|
}
|
|
|
|
public function priceInput(): Attribute
|
|
{
|
|
return Attribute::make(
|
|
get: fn() => (string) $this->price,
|
|
);
|
|
}
|
|
|
|
// 4. Relation
|
|
public function variant(): BelongsTo
|
|
{
|
|
return $this->belongsTo(ProductVariant::class, 'variant_id')->withTrashed();
|
|
}
|
|
}
|