Item.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Collection;
  4. use Illuminate\Database\Eloquent\Model;
  5. class Item extends Model
  6. {
  7. public $timestamps = true;
  8. protected $guarded = ['id'];
  9. protected $fillable = [
  10. 'caption',
  11. 'category_id',
  12. 'subcategory_id',
  13. 'user_id',
  14. 'description',
  15. 'pictures',
  16. ];
  17. public function user()
  18. {
  19. return $this->belongsTo('App\User');
  20. }
  21. public function subcategory()
  22. {
  23. return $this->belongsTo('App\Models\Subcategory');
  24. }
  25. public function category()
  26. {
  27. return $this->belongsTo('App\Models\Category');
  28. }
  29. public function picture()
  30. {
  31. return $this->hasMany(Picture::class);
  32. }
  33. public function getPictures(): Collection
  34. {
  35. return Picture::where('item_id', '=', $this->id)->get();
  36. }
  37. public function getPicturesPath(): array
  38. {
  39. if ($this->picturePath){
  40. return $this->picturePath;
  41. }
  42. $picturesPath = [];
  43. foreach ($this->getPictures() as $picture) {
  44. $picturesPath[] = $picture->path;
  45. };
  46. return $this->picturePath = $picturesPath;
  47. }
  48. }