To query the payment_date field in the payments table, you need to adjust your filter to correctly reference the nested relationship. Since you are dealing with a nested relationship (dues.payments), you cannot directly use whereBetween on the dues.payments.payment_date as if it were a column on the dues table.
Instead, you should use the whereHas method to apply a constraint to the payments relationship. Here's how you can modify your query:
return Inertia::render('Reports/NRA', [
'payments' => NRAPaymentResource::collection(
QueryBuilder::for(User::class)
->whereRelation('dues', 'nra_dues', '>', 0)
->with('dues.payments')
->with('profile')
->allowedFilters([
AllowedFilter::callback('between', function (Builder $query, $value) {
$query->whereHas('dues.payments', function (Builder $query) use ($value) {
$query->whereBetween('payment_date', [$value[0], $value[1]]);
});
})
])
->paginate(20)
->withQueryString()
)
]);
In this solution, the whereHas method is used to add a constraint to the payments relationship that is nested within the dues relationship. The whereBetween method is then used within the closure to filter the payment_date within the specified range.
This should resolve the error you're encountering and allow you to filter the payment_date from the payments table correctly.