Answer the question
In order to leave comments, you need to log in
How to get data in model attributes from related Laravel table and output that json without error?
Hey!
Faced an interesting problem in Laravel.
There are two models Item and ItemImages.
I need to get json with all items, everything works until the 'photo' attribute appears (first photo)
class Item extends Model
{
protected $appends = [
'photo',
];
public function imgs()
{
return $this->hasMany(ItemImage::class);
}
public function getPhotoAttribute()
{
$img = $this->imgs->first();
return $img->src;
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question