CommonCatalogItem.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Models;
  4. use Database\Factories\CommonCatalogItemFactory;
  5. use Illuminate\Database\Eloquent\Casts\Attribute;
  6. use Illuminate\Database\Eloquent\Factories\HasFactory;
  7. use Illuminate\Database\Eloquent\Model;
  8. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  9. use Illuminate\Database\Eloquent\Relations\BelongsToMany;
  10. use Illuminate\Database\Eloquent\SoftDeletes;
  11. class CommonCatalogItem extends Model
  12. {
  13. /** @use HasFactory<CommonCatalogItemFactory> */
  14. use HasFactory;
  15. use SoftDeletes;
  16. public const DEFAULT_SORT_BY = 'article';
  17. protected $fillable = [
  18. 'image_file_id',
  19. 'article',
  20. 'calculator_name',
  21. 'kind',
  22. 'dimensions',
  23. 'fall_height',
  24. 'additional_info',
  25. 'dimension_unit',
  26. 'weight',
  27. 'volume',
  28. 'places',
  29. 'composition',
  30. 'age_group',
  31. 'max_users',
  32. 'unit',
  33. 'series',
  34. 'trademark',
  35. 'note',
  36. ];
  37. protected $appends = ['image'];
  38. protected function casts(): array
  39. {
  40. return [
  41. 'fall_height' => 'decimal:3',
  42. 'weight' => 'decimal:3',
  43. 'volume' => 'decimal:3',
  44. 'places' => 'integer',
  45. 'max_users' => 'integer',
  46. ];
  47. }
  48. public function imageFile(): BelongsTo
  49. {
  50. return $this->belongsTo(File::class, 'image_file_id');
  51. }
  52. public function documents(): BelongsToMany
  53. {
  54. return $this->belongsToMany(
  55. File::class,
  56. 'common_catalog_item_documents',
  57. )->withTimestamps();
  58. }
  59. protected function image(): Attribute
  60. {
  61. return Attribute::make(
  62. get: fn (): string => (string) ($this->imageFile?->thumbnail_link ?? ''),
  63. );
  64. }
  65. }