Reputation: 123
I got 2 main tables users and pictures. Each user can have same picture(and vice versa) so it have got "hasMany"(and pivot table in between of them) relationship in Elaquent and it works well. Also, I do have as separate table pictures_details. I wonder, is it possible to get the details of pictures_details when accessing from user to picture model?
public function getImages($id, Images $images)
{
return $users->with('images')->find($id);
}
So when I have call like that, can I also get data of pictures_details table? I have following method in my Image model
public function imageDetails(): BelongsTo
{
return $this->belongsTo(ImageDetails::class, 'image_details_id', 'id');
}
So I thought something like
return $users->with('images')->find($id)->with('imagedetails');
Will work but its not. Can you tell me how to achieve this? Or it is wrong approach?
Upvotes: 0
Views: 44
Reputation: 397
I want to edit ArSeN's answer. May be it would be more correct if
$users->with(['images.imagedetails'])->find($id);
Upvotes: 1
Reputation: 5248
find($id)
returns a single model, not the eloquent query, you cant do joins there.
Switch them around:
$users->with('images')->with('imagedetails')->find($id);
Upvotes: 1