2017-07-18 64 views
0

我正在開發網上商店。我在我的模型中有以下關係。從關係表中獲取數據

shoppingCart -> hasMany -> shoppingCartItem 
shoppingCartItem -> belongTo -> shoppingCart 

我在我的控制器功能來獲得的product_id

$product = Product::find($request->get('product_id'))->first(); 

產品表我與圖像的關係。我將圖像存儲在只有product_id和image_id的不同表格中。

現在的問題是可以我得到

public function getCart(Request $request) 
{ 
    // get the shoppingCart 
    $shoppingCart = $this->loadShoppingCart($request->get('key')); 
    $carts = [ 
     'amount' => $shoppingCart->total_products, 
     'price'=>$shoppingCart->total_price 
    ]; 
    // get theimage of the product 
    $productImage = Image::with('image_id', $request->get('image_id')); 

    return response()->json(['carts','productImage']); 
} 
+0

我不知道你的問題,你想得到什麼? –

+0

我修正了這個問題。 $ shoppingCart = ShoppingCart :: where('key',$ key) - > with('shoppingCartItems','shoppingCartItems.product','shoppingCartItems.product.images') - > first(); –

+0

所以這個問題已經修復? –

回答

0

我定這樣的。

$shoppingCart = ShoppingCart::where('key' ,$key)->with('shoppingCartItems', 'shoppingCartItems.product', 'shoppingCartItems.product.images')->first();