TypeMember.php 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. 'max',
  16. ];
  17. protected function updatedAt(): Attribute
  18. {
  19. return Attribute::make(
  20. get:fn($value) => Carbon::parse($value)->translatedFormat('l d/m/Y')
  21. );
  22. }
  23. protected function type(): Attribute
  24. {
  25. return Attribute::make(
  26. set:fn($value) => ucwords($value)
  27. );
  28. }
  29. protected function price(): Attribute
  30. {
  31. return Attribute::make(
  32. get:fn($value) => (new CurrencyFormatService)->setRupiahFormat($value, true)
  33. );
  34. }
  35. public function scopeFilter($query, $id)
  36. {
  37. $query->when($id ?? null, function ($query, $id) {
  38. $query->where('id', $id);
  39. });
  40. }
  41. }