TypeMember.php 1.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Models;
  3. use App\Services\CurrencyFormatService;
  4. use Carbon\Carbon;
  5. use Illuminate\Database\Eloquent\Casts\Attribute;
  6. use Illuminate\Database\Eloquent\Factories\HasFactory;
  7. use Illuminate\Database\Eloquent\Model;
  8. class TypeMember extends Model
  9. {
  10. use HasFactory;
  11. protected $fillable = [
  12. 'type',
  13. 'description',
  14. 'price',
  15. ];
  16. protected function updatedAt(): Attribute
  17. {
  18. return Attribute::make(
  19. get:fn($value) => Carbon::parse($value)->translatedFormat('l d/m/Y')
  20. );
  21. }
  22. protected function type(): Attribute
  23. {
  24. return Attribute::make(
  25. set:fn($value) => ucwords($value)
  26. );
  27. }
  28. protected function price(): Attribute
  29. {
  30. return Attribute::make(
  31. get:fn($value) => (new CurrencyFormatService)->setRupiahFormat($value, true)
  32. );
  33. }
  34. public function scopeFilter($query, $id)
  35. {
  36. $query->when($id ?? null, function ($query, $id) {
  37. $query->where('id', $id);
  38. });
  39. }
  40. }