1

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
BobB
  • 750
  • 1
  • 9
  • 22

2 Answers2

2

You can eager load the person data all the time you call faces on the Photo's model:

// Photo.php
public function faces()
{
    return $this->hasMany(Face::class)->with('person');
}

Or in your query, you can do this to eager load only at that time:

$photos = Photo::with('faces', 'faces.person')->paginate();

Now you can access like this:

$photos->first()->faces->first()->person->name; // will print the name of the person of the first face of the first photo...

I hope this answer can be helpful.

Ian Rodrigues
  • 743
  • 5
  • 21
  • when i dd($photo->face) I get this: Collection {#355 ▼ #items: array:1 [▼ 0 => Face {#376 ▼ #table: "faces" #attributes: array:13 [▼ "id" => 181 "photo_id" => 16 "people_id" => 1 ] #relations: array:1 [▼ "person" => null ] } ] } Notice Person is Null. – BobB Aug 24 '17 at 16:19
  • Thanks for your help. My issue was people_id should be person_id. Laravel takes the model name not the table name – BobB Aug 24 '17 at 16:26
  • Great @BobB, I think would be helpful if you update your question. – Ian Rodrigues Aug 24 '17 at 16:40
  • Still having this fail dd($photo->faces->person); Am I missing something as dd($photo->faces); has the correct data for person – BobB Aug 24 '17 at 16:44
  • Pastebin it your `dd($photo->faces);` – Ian Rodrigues Aug 24 '17 at 16:45
  • This is strange, it seems it could be accessible through `$photo->faces->person` – Ian Rodrigues Aug 24 '17 at 17:00
  • Agreed. Property [person] does not exist on this collection instance. Strange. – BobB Aug 24 '17 at 17:03
  • @BobB We are tying to get `persons` on a Collection, do this: `$photo->faces->first()->person` – Ian Rodrigues Aug 24 '17 at 17:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/152783/discussion-between-ian-rodrigues-and-bobb). – Ian Rodrigues Aug 24 '17 at 17:09
0

Try to get the person of the face by changing your query like so:

Photo::with(['faces' => function($query) {$query->with('person')}])->paginate();

I am not quite sure about the syntax, but this is how you can nest relations in an eloquent model. A shorter way to write this might be: Photo::with('faces.person')->paginate();

Some more information is provided here.

avi92
  • 11
  • 1
  • 3