2016-04-06 4 views
0

Ich arbeite durch das Laracasts Video über egar loading und so weit so gut!Wie kann ich Tabellenwerte aus dem Eager laden?

Ich dachte, ich würde die Konzepte gut verstehen, aber jetzt habe ich aufgelegt, wie ich es gemacht habe (CodeIgniter) vs diese ziemlich tolle Art und Weise.

Ich habe 3 Tabellen:

cards  #card_id, title, created_at, updated_at 
notes  #note_id, name, created_at, updated_at 
card_notes #card_id, note_id 

Innerhalb card_notes gibt es Felder FK:

card_id #FK back to cards->card_id 
note_id #FK back to notes->note_id 

ich in der Lage bin alle Noten für eine bestimmte Karte zu bekommen:

CardsController.php

public function show(Card $card) 
{ 
    $card->load('notes'); 
} 

Card.php

public function notes() 
{ 
    return $this->hasMany(CardNote::class); 
} 

json

{ 
    id: 1, 
    title: "First Card", 
    created_at: null, 
    updated_at: null, 
    notes: [ 
     { 
      card_id: 1, 
      note_id: 1, 
      created_at: null, 
      updated_at: null 
     }, 
     { 
      card_id: 1, 
      note_id: 2, 
      created_at: null, 
      updated_at: null 
     } 
    ]    
} 

So weit, so gut! Jetzt, innerhalb meiner notes Tabelle, habe ich eine name Spalte, die ich zeigen möchte. Hier stecke ich fest. Ich bin in der Lage, zu meiner Nachschlagetabelle zu gelangen, aber ich habe nicht herausgefunden, wie man hinter diese Tabelle und in die verbundene Tabelle bekommt. Es sieht so aus als ob ich hasManyThrough brauche aber ... ich falle flach auf mein Gesicht.

return $this->hasManyThrough(Note::class, CardNote::class); 

Ich brauche alle Noten Aufzeichnungen für die gegebenen note_id zu bekommen, aber ich bin nicht sicher, wie man in das nächste Level zu Drilldown. Das ist, was ich versuche am Ende zu erzeugen:

json

{ 
    id: 1, 
    title: "First Card", 
    created_at: null, 
    updated_at: null, 
    notes: [ 
     { 
      card_id: 1, 
      note_id: 1, 
      name: "John Doe" 
      created_at: null, 
      updated_at: null 
     }, 
     { 
      card_id: 1, 
      note_id: 2, 
      name: "Jane Doe" 
      created_at: null, 
      updated_at: null 
     } 
    ]    
} 

Vielen Dank für Ihre Anregungen!

Antwort

0

Es erweist sich als hilfreich, wenn Sie weiter scrollen. Ich wusste, dass ich nahe bei hasManyThrough war, ich habe gerade die optionalen Parameter verpasst.

return $this->hasManyThrough(Note::class, CardNote::class, 'card_id', 'note_id');