Reputation: 666
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 .
Upvotes: 1
Views: 116
Reputation: 989
With the code as it is now, these are the queries being executed (shown to me by the Laravel Debugbar):
select * from `carts` where `user_id` = 1
select * from `products` where `id` = 15
select * from `product_details` where `product_id` = 15 and `id` = 33
select * from `products` where `id` = 20
select * from `product_details` where `product_id` = 20 and `id` = 41
It looks like a separate query is being executed for every single product
and product_details
record; not very efficient. Let's try this:
$carts = Cart::where('user_id', Session::get('user_id'))
->with('product.details')
->get();
For the above statement the following Eloquent relationships are needed:
// Cart model
public function product() {
return $this->belongsTo('App\Product');
}
// Product model
public function details() {
return $this->hasOne('App\ProductDetails', 'product_id', 'id');
}
This looks a lot better; no matter how many product or product detail records are retrieved, no more than 3 queries are executed.
select * from `carts` where `user_id` = 1
select * from `products` where `products`.`id` in (15, 20)
select * from `product_details` where `product_details`.`product_id` in (15, 20)
Now to combine the model attributes with those of its relations:
$data = [];
foreach ($carts as $key => $cart) {
$data[$key] = [
'product_name' => $cart->product->product_name,
'product_image' => $cart->product->product_image,
'product_id' => $cart->product_id,
'variation_id' => $cart->variation_id,
'original_price' => $cart->product->details->original_price,
'selling_price' => $cart->product->details->selling_price,
'discount_price' => $cart->product->details->discount_price,
'discount_percent' => $cart->product->details->discount_percent,
'product_stock' => $cart->product->details->product_stock,
'product_unit' => $cart->product->details->product_unit,
'product_unit_value' => $cart->product->details->product_unit_value,
'cart_quantity' => $cart->quantity,
];
}
Upvotes: 3