Hi, you should try
$vouchers = Voucher::with('cart.lineItems');
Since the cart model is the one with the lineItems relationship and not the Voucher model. You can then chain the rest of the methods like so
$vouchers = Voucher::with('cart.lineItems')
->whereBetween('vouchers.vchlastmodmsec', [$fromDate, $toDate])
->groupBy(['redeemed_date', 'order_date', 'order_id', 'settlement_status'])
I'm pretty sure you can use ```when()`` instead of the IF statements also if you prefer
see more here https://laraveldaily.com/less-know-way-conditional-queries/
So it looks something like this...
$query = Voucher::with('cart.lineItems')
->whereBetween('vouchers.vchlastmodmsec', [$fromDate, $toDate])
->groupBy(['redeemed_date', 'order_date', 'order_id', 'settlement_status']);
$query->when($request->filled('brand') && $request->input('brand'), function ($query) use($request) {
return $query->where('carts.ca_brandid', $request->input('brand'));
})
$query->when($request->filled('city') && $request->input('city'), function ($query) use($request) {
return $query->where('carts.ca_redmcitycode', $request->input('city'));
});
$vouchers = $query->get();