I am struggling to merge these two queries into a single query.
I have tried joins and subquery but could solve this.
I am using Laravel, I am executing one query in the Controller and the other in the blade file.
Controller code:
$orders = Order::select([
DB::raw('id'),
DB::raw('wp_order_id'),
DB::raw('customer_name'),
DB::raw('pickup_location'),
DB::raw('vendor_id'),
DB::raw('date_created'),
])
->where('orders.store_id', $vendor->store_id)
->where('orders.vendor_id', $vendor->wp_vendor_id)
->whereIn('status', ['processing', 'pending'])
->groupBy(['wp_order_id'])
->get();
Blade code:
@forelse ($orders as $item)
<tr>
<td>
@php
$products = \App\OrderProduct::select([
DB::raw('product_name'),
DB::raw('count(product_id) as number_of_items'),
])
->where('wp_order_id', $item->wp_order_id)
->groupBy('product_id')
->get();
@endphp
@forelse ($products as $product)
{{ $product->number_of_items . ' x ' . $product->product_name }} <br>
@empty
@endforelse
</td>
</tr>
@empty
@endforelse
Kindly help me solve this.