RelationsShips Larval 5.4 Accessing search data with with?

2

I have a relationship of One To Many , I wanted to know how do I access data from the two tables.

See:

Album::with('imagemAlbums')->where('departamento_id', $id)->get();

Return from dd()

  

Now in my View I want to access the data from the table Some and the table imagemAlbum where imagemAlbum is related. When I do foreach I access the data from Album , and how do I access the other data from the other table which in this case is imagemAlbum ?

    
asked by anonymous 24.08.2017 / 02:07

1 answer

3

This way:

$albuns = Album::with('imagemAlbums')
               ->where('departamento_id', $id)->get();

foreach($albuns as $album)
{
     $album-> ... ; // campos
     foreach($album->imagemAlbum as $imagem)
     {
         $imagem-> ... ; // campos
     }
}

Why does this happen?

The results obtained are a collection of Album , which in every Album has a collection of ImageAlbum .

24.08.2017 / 02:12