I have three models
Photo
-id
-path_full
Person
-id
-name
Face
-id
-person_id //was people_id
-photo_id
I am trying to access the person name from the Photo model.
Face Model:
public function photo()
{
return $this->belongsTo(Photo::class);
}
public function person()
{
return $this->belongsTo(Person::class);
}
Photo Model:
public function faces()
{
return $this->hasMany(Face::class);
}
Person Model:
public function faces()
{
return $this->hasMany(Face::class);
}
In my controller I load the Photos like this:
$photos = Photo::with('faces')->paginate();
In my blade template I want to access the name of the face in the photo.
I got this far.
This is in a foreach hence singular $photo:
{{implode($photo->faces->pluck('people_id')->toArray(),', ')}}
How can I get the name of the person instead?
Solution
I needed this in my view and note my change to the db to person_id so eloquent could do it's magic.
//Controller:
$photos = Photo::with('faces.person')->paginate();
//View:
@foreach($photo->faces as $face)
{{$face->person['name']}}
@endforeach