Laravel eloquent Modell, wie man Daten von der Beziehung der Tabelle

Ich bin die Entwicklung einer laravel-Applikation, die die folgenden beredten Modelle

  • Produkt hasMany('App/Sku','products_id')
  • Sku belongTO('App/Product')

Ich habe einen controller "ProductController', wo der folgende code ist verfügbar

public function index()
{    
    $products = Product::all();
    foreach($products as $product){
            $products_id = $product->products_id;
    }
}

Ich bin freilegen Erholsamen API wird es meinen Benutzer, um alle Produkt-details (einschließlich skus, Versandarten etc..).

Nehme an, wenn ich eine API - BEKOMMEN : /Produkte

Den code, holt alle Angaben zum Produkt werden, was einige der folgenden

public function index()
{              
      $products = Product::all();
      foreach($products as $product){
          $products_id = $product->products_id;
          $skus_data = Product::find($products_id)->skus;
      }
        //Now I have both the product details + skus which I can bundle into an array/json.
}

Nun meine Frage , ist diese Logik richtig? In diesem Fall werden alle Logiken sind in der Steuerung, da ich mit eloquent-Modelle habe ich ein Modell für jede Tabelle und die Beziehungen definiert sind, die in es. Gibt es eine Möglichkeit, ich kann alle details eines Produkts/verknüpften Modell (Produkte details (in Tabelle 1)+ Sku-Angaben (in Tabelle 2)) anstatt die unten

foreach($products as $product){
    $products_id = $product->products_id;
    $skus_data = Product::find($products_id)->skus;
}

Ich bin ziemlich neu in laravel Entwicklung und eloquent Modelle. Ich werde mit Hilfe von repository-pattern für die Entwicklung und in diesem Fall, wo nicht die aboe Logik (Produkt+Sku Kombination) befindet.

Bitte helfen.

InformationsquelleAutor Ajeesh | 2015-12-04
Schreibe einen Kommentar