Out-put
array:2 [▼
0 => array:1 [▼
0 => array:12 [▼
"product_name" => "Raw Mango"
"product_image" => "Raw_Mango_1997.jpg"
"product_id" => "15"
"variation_id" => "33"
"original_price" => "15.31"
"selling_price" => "12.25"
"discount_price" => "3.06"
"discount_percent" => "20"
"product_stock" => "available"
"product_unit" => "gram"
"product_unit_value" => "250"
"cart_quantity" => "1"
]
]
1 => array:1 [▼
0 => array:12 [▼
"product_name" => "Banana - Yelakki"
"product_image" => "Banana_-_Yelakki_9339.jpg"
"product_id" => "20"
"variation_id" => "41"
"original_price" => "56.25"
"selling_price" => "45"
"discount_price" => "11.25"
"discount_percent" => "20"
"product_stock" => "available"
"product_unit" => "gram"
"product_unit_value" => "500"
"cart_quantity" => "1"
]
]
]
How to display this type of data in laravel blade page.I want to get data in object to show the data in laravel view page .I try many method but do not get solution please help me
CartController.php
public function viewCart(){
$select=DB::table('carts')->where('user_id','=',Session::get('user_id'))->get();
$count=count($select);
if($count>0)
{
foreach($select as $row1)
{
$product_id = $row1->product_id;
$variation_id = $row1->variation_id;
$quantity = $row1->quantity;
$sql=DB::table('products')->where('id','=',$product_id)->get();
$arr=array();
foreach($sql as $res)
{
$product_name = $res->product_name;
$image = $res->product_image;
$product_desc = $res->product_desc;
$sql1=DB::table('product_details')->where('product_id','=',$product_id)->where('id','=',$variation_id)->get();
foreach($sql1 as $res1)
{
$original_price = $res1->original_price;
$selling_price = $res1->selling_price;
$discount_price = $res1->discount_price;
$discount_percent = $res1->discount_percent;
$product_stock = $res1->product_stock;
$product_unit = $res1->product_unit;
$product_unit_value = $res1->product_unit_value;
$item_array=array(
'product_name' =>$product_name,
'product_image' => $image,
'product_id' => $product_id,
'variation_id' => $variation_id,
'original_price' => $original_price,
'selling_price' => $selling_price,
'discount_price' => $discount_price,
'discount_percent' => $discount_percent,
'product_stock' => $product_stock,
'product_unit' => $product_unit,
'product_unit_value' => $product_unit_value,
'cart_quantity' => $quantity
);
array_push($arr, $item_array);
}
$rows['product'][]=$arr;
}
}
return View('cart-view',['products'=> $rows]);
}
else
{
return view('cart-view');
}
}
I am trying to get user added cart details on page but when try foreach then its gives to error. how to iterete this type of data in laravel view page please help me .
$cartItems = Cart::where('user_id', session()->get('user_id'))->with(['products']);if models are created in a standard way with proper relationship definitions. And instead using a separate product_details table, you should be using the main products table and use a table to store meta info if needed. In short, you should be doing this the laravel way if you are using this framework.