I have the following tables.
basket_items:
id | basket_id | product_id
-----------------------------
1 | 4 | 1
2 | 4 | 2
3 | 4 | 1
4 | 4 | 1
5 | 4 | 1
prepaid_credits:
id | order_item_id | product_id | quantity
----------------------------------------------
1 | 2 | 1 | 1
1 | 3 | 1 | 2
I get a list of prepaid credits as follows which tells me user 1 has 3 credit available for product_id 1
$prepaidCredits = DB::table('orders')
->join('order_items', 'orders.id', '=', 'order_items.order_id')
->join('prepaid_credits', 'order_items.id', '=', 'prepaid_credits.order_item_id')
->where('orders.user_id', '=', 1)
->select(DB::raw('SUM(prepaid_credits.quantity) as quantity, prepaid_credits.product_id'))
->groupBy('prepaid_credits.product_id')
->havingRaw('SUM(prepaid_credits.quantity) > 0')
->get();
I get a list of basket items as follows:
$basketItems = DB::table('basket_items')->where('basket_id', '=', 4)->get();
Now I want to create two new collections. One collection of basket items where the user can use an available credit and another collection where the user has to pay. How do I loop through and check the basketItems collection with the prepaidCredits collection to give me the above? So in the example above I should end up with $basketItemsUseCredits with basket_items with id's 1,3 and 4. $basketItemsPay should end up with basket_items with id's 2 and 5.
$basketItemsUseCredits = $basketItems->filter(function ($basketItem) {
// return items where user has pre paid credits for that product
})
$basketItemsPay = $basketItems->filter(function ($basketItem) {
// return items where user has no pre paid credits
})
Any help appreciated.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire