2016-06-16 10 views
0

注文コード - 製品 という名前のテーブルが3つあり、トラッキングコードがあります。ファクタiにはproduct_idとorder_idがあります。 注文商品を受け取るにはどうしたらいいですか? :|laravel 5.1リレーションに関する問題

例えば:order:where('trackingCode','HashCode')->ProductsInFactore()->get();

注文:

Schema::create('order', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('name'); 
     $table->string('family'); 
     $table->string('email'); 
     $table->string('phone'); 
     $table->string('state'); 
     $table->string('city'); 
     $table->string('address'); 
     $table->string('PostalCode'); 
     $table->boolean('status')->default(0); 
     $table->string('TrackingCode'); 
     $table->timestamps(); 
    }); 

Factore:

Schema::create('factor', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->integer('count'); 
     $table->integer('order_id')->unsigned(); 
     $table->integer('product_id')->unsigned(); 
     $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade'); 
     $table->foreign('order_id')->references('id')->on('order')->onDelete('cascade'); 
     $table->timestamps(); 
    }); 

や製品

注文モデル

public function products() 
{ 
    return $this->hasManyThrough('App\products', 'App\factor','order_id','id','product_id'); 
} 

コントローラ

$order = order::find(23)->products; 
dd($order); 

答えて

0

あなたにモデルでの関係を定義した場合、 "持っていて、多くの" 関係(laravelのドキュメントを参照してください)、あなたはただ行うことができます

$order = Order::find($id); 
$products = $order->products; 

特定の順序に属するすべての製品を取得します。

関連する問題