I have the following structure
Orders
- id
- users_id
- orderPaid (can be 1 or 0)
OrdersItems
- id
- order_id
- events_id
- qty
events
- id
- name
- (and others)
Relationships
One Order hasMany Orderitems. One Orderitem belongsTo Order.
One Event hasMany Orderitems. One Orderitem belongsTo Event.
What I need to do is:
Get all the Events with ALL paid(!) qty's. So if i have 2 orders with two items each (total 4) but just one order is paid, qty should show = 2 instead of 4
Code so far:
$events = Main::with(['clients','events.orderitems'])->where('id', $id)->first();
foreach($events->events as $events) {
$events->orderscount = count($events->orderitems);
}
gives me successfully the total qty, but I just need the qty if orderPaid = 1
How can I do that? Every help is every appreciated
Try using Eloquent eager load constraints (this will add a subquery):
$events = Main::with(['clients', 'events.orderitems' => function($query) {
$query->whereIn('order_id', function($query) {
$query->select('id')->from('orders')->where('orderPaid', 1);
});
}])->where('id', $id)->first();
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments