php - Laravel 4 many to many relationships -


We say we have to follow tables:

game

question

Now I have received the following controller snippet to get a question related to a single game:

  if ($ Code) with {$ gamedata = Game :: ('question') - & gt; Where ('secretcode', $ code) - & gt; get (); } And {$ gamedata = Game :: with ('question') - & gt; get (); }  

This is my game model:

  Class games expandable prominently {/ * Table Definition * / Protected $ table = 'Games'; Public Function Questions () {Return $ this- & gt; Association value ('question', 'sports', 'game_id', 'question_id');  
  / * table definition * / protected $ table = 'question'; // question relation public work answer () {return $ this- & gt; Hamind ('answer', 'question_id', 'id'); }  

}

Now the game is getting and their related questions start working with this code. Although I would like to include the answer too. How do I modify this code to adjust for another foreign key relationship yet?

I believe that you can put the relationship in the nest;

  if ($ code) {$ gamedata = game :: with ('questions' answer') - & gt; Where ('secretcoded', $ code) - & gt; get (); } And {$ gamedata = game :: with ('questions.answers') - & gt; get (); }  

View docs - search for nested relationships

$ books = book :: with ('author.contacts') -> get ();

In the above example, the author will relish the relationship, and the author's contact relations will also be loaded.


Comments