1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Collection;
- use Illuminate\Database\Eloquent\Model;
- class Item extends Model
- {
- public $timestamps = true;
- protected $guarded = ['id'];
- protected $fillable = [
- 'caption',
- 'category_id',
- 'subcategory_id',
- 'user_id',
- 'description',
- 'pictures',
- ];
- public function user()
- {
- return $this->belongsTo('App\User');
- }
- public function subcategory()
- {
- return $this->belongsTo('App\Models\Subcategory');
- }
- public function category()
- {
- return $this->belongsTo('App\Models\Category');
- }
- public function picture()
- {
- return $this->hasMany(Picture::class);
- }
- public function getPictures(): Collection
- {
- return Picture::where('item_id', '=', $this->id)->get();
- }
- public function getPicturesPath(): array
- {
- if ($this->picturePath){
- return $this->picturePath;
- }
- $picturesPath = [];
- foreach ($this->getPictures() as $picture) {
- $picturesPath[] = $picture->path;
- };
- return $this->picturePath = $picturesPath;
- }
- }
|