Larval Relationsships 5.4 accessing search data with?

Asked

Viewed 26 times

2

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

Behold:

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

Return of dd()

inserir a descrição da imagem aqui

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

1 answer

3


Thus:

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

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

Why does that happen?

The results obtained are a collection of Album, that in each Album has a collection of Imagemalbum.

References:

  • 1

    Perfect explanation. Thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.