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