MafOrder.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Models;
  3. use App\Models\Scopes\YearScope;
  4. use Illuminate\Database\Eloquent\Attributes\ScopedBy;
  5. use Illuminate\Database\Eloquent\Casts\Attribute;
  6. use Illuminate\Database\Eloquent\Model;
  7. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  8. use Illuminate\Database\Eloquent\SoftDeletes;
  9. #[ScopedBy([YearScope::class])]
  10. class MafOrder extends Model
  11. {
  12. use SoftDeletes;
  13. // set year attribute to current selected year
  14. protected static function boot(): void
  15. {
  16. parent::boot();
  17. static::creating(function($attributes) {
  18. if(!isset($attributes->year)) {
  19. $attributes->year = year();
  20. }
  21. });
  22. }
  23. protected $fillable = [
  24. 'year',
  25. 'order_number',
  26. 'user_id',
  27. 'product_id',
  28. 'quantity',
  29. 'in_stock',
  30. ];
  31. const DEFAULT_SORT_BY = 'created_at';
  32. public function user(): BelongsTo
  33. {
  34. return $this->belongsTo(User::class);
  35. }
  36. public function product(): BelongsTo
  37. {
  38. return $this->belongsTo(Product::class);
  39. }
  40. }