2017-07-18 65 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();