Reputation: 1056
public function getCartItemsCount(Request $request) {
$product_count = Product::whereIn(
'id', Auth::user()
->getCartItems
->pluck('product_id'))
->get();
$quantity = Cart::where('user_id', Auth::id())->get();
return $quantity->merge($product_count);
}
It returns like this:
[ {
"id": 1,
"user_id": 11,
"quantity": 1,
"product_id": 9,
"created_at": "2021-06-20T23:11:08.000000Z",
"updated_at": "2021-06-20T23:11:09.000000Z"
}, {
"id": 2,
"user_id": 11,
"quantity": 4,
"product_id": 3,
"created_at": "2021-06-20T23:11:15.000000Z",
"updated_at": "2021-06-20T23:11:16.000000Z"
}, {
"id": 3,
"name": "Den's Ultimate Overload",
"brand": "Den's Cafe",
"category": "Food",
"sub_category": null,
"price": 374,
"average_rating": 5,
"created_at": "2021-06-23T23:52:54.000000Z",
"updated_at": "2021-06-23T23:52:55.000000Z"
}, {
"id": 9,
"name": "French Fries",
"brand": "Jollibee",
"category": "Food",
"sub_category": null,
"price": 35,
"average_rating": 4,
"created_at": "2021-06-23T23:53:00.000000Z",
"updated_at": "2021-06-23T23:53:01.000000Z"
} ]
Objective: the data should be like this
[
{
"id": 3,
"name": "Den's Ultimate Overload",
"quantity:" 4,
"brand": "Den's Cafe",
"category": "Food",
"sub_category": null,
"price": 374,
"average_rating": 5,
"created_at": "2021-06-23T23:52:54.000000Z",
"updated_at": "2021-06-23T23:52:55.000000Z"
}, {
"id": 9,
"name": "French Fries",
"quantity:" 1,
"brand": "Jollibee",
"category": "Food",
"sub_category": null,
"price": 35,
"average_rating": 4,
"created_at": "2021-06-23T23:53:00.000000Z",
"updated_at": "2021-06-23T23:53:01.000000Z"
} ]
Note: the quantity must be assign with the same product id and id
Upvotes: 2
Views: 92
Reputation: 1056
public function getCartItemsCount(Request $request) {
$product_count = Product::whereIn('id', Auth::user()->getCartItems->pluck('product_id'))->orderBy('brand', 'asc')->get();
$quantity = Cart::where('user_id', Auth::id())->get();
foreach($product_count->toBase('product_id')->concat($quantity) as $product) {
if($product->product_id) {
$new = $product_count->where('id', $product->product_id)->first();
$new->quantity = $product->quantity;
}
}
// this return a data wherein it merge with matching the product id and the id. this is use for mergin the data of users added to cart and product details. For ecommerce.
return $product_count;
}
i got the answer after so many trials. Note: you can use pluck method to get data that you need.
Upvotes: -1
Reputation: 161
You can use array_replace_recursive method like,
$combined = array_replace_recursive($quantity, $product_count);
it return values which you want.
/* for an example
$color = array(
array('id' => 1, 'color' => 'red'),
array('id' => 2, 'color' => 'green'),
array('id' => 3, 'color' => 'blue')
);
$size = array(
array('id' => 1, 'size' => 'SM'),
array('id' => 2, 'size' => 'XL'),
array('id' => 3, 'size' => 'MD'),
array('id' => 4, 'size' => 'LG'),
);
$merged = array_replace_recursive($color, $size);
var_dump($merged);
exit;
Output of this like:
Array (
[0] => Array ( [id] => 1 [color] => red [size] => SM )
[1] => Array ( [id] => 2 [color] => green [size] => XL )
[2] => Array ( [id] => 3 [color] => blue [size] => MD )
[3] => Array ( [id] => 4 [size] => LG )
)
*/
Upvotes: 1