Car.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. /*
  3. * Car — модель автомобиля в каталоге.
  4. *
  5. * Создана: 2026-05-06 | Таблица: cars (миграция: 2026_05_06_151148_create_cars_table.php)
  6. * $fillable — все поля, кроме id/timestamps (mass assignment через Car::create/update)
  7. * $casts — автоматическое преобразование типов: boolean-поля, JSON-массивы (options, photos_gallery)
  8. * Акцессоры getXxxAttribute() — виртуальные поля для отображения меток в шаблонах
  9. */
  10. namespace App\Models;
  11. use Illuminate\Database\Eloquent\Model;
  12. class Car extends Model
  13. {
  14. protected $fillable = [
  15. 'status', 'condition', 'make', 'model', 'generation', 'year',
  16. 'vin', 'plate', 'body_type', 'doors', 'color_exterior', 'color_interior',
  17. 'engine_type', 'engine_volume', 'engine_power_hp', 'transmission', 'drive',
  18. 'mileage_km', 'steering', 'owners_count', 'customs_cleared', 'pts', 'accident_free',
  19. 'price_usd', 'price_rub', 'price_vladivostok', 'price_moscow', 'price_negotiable',
  20. 'country_origin', 'city', 'platform', 'options', 'title', 'description',
  21. 'photo_main', 'photos_gallery',
  22. ];
  23. protected $casts = [
  24. 'customs_cleared' => 'boolean',
  25. 'accident_free' => 'boolean',
  26. 'price_negotiable' => 'boolean',
  27. 'photos_gallery' => 'array',
  28. 'options' => 'array',
  29. ];
  30. // Акцессор: возвращает читаемую русскую метку статуса для шаблонов ($car->status_label)
  31. public function getStatusLabelAttribute(): string
  32. {
  33. return match ($this->status) {
  34. 'active' => 'Активен',
  35. 'sold' => 'Продан',
  36. 'draft' => 'Черновик',
  37. default => $this->status,
  38. };
  39. }
  40. // Акцессор: возвращает "Новый" или "Б/У" ($car->condition_label)
  41. public function getConditionLabelAttribute(): string
  42. {
  43. return $this->condition === 'new' ? 'Новый' : 'Б/У';
  44. }
  45. }