I have been having this problem for few days, and there were no documentation on Laravel web or any issue related on google search. So I was wondering if it is possible or if I could do it any other way except loading all data from database to a variable and do the filtering.
Fetching relations with an addition to another parent table(cooperatives.next_due_date) CODE
$coperatives = Cooperative::withNextDueDate()
->has("shares")
->with(["shares.lastDue" => function ($q) {
$q->whereRaw("due_in <> cooperatives.next_due_date");
}])
->get();
Returned Error
Unknown column 'cooperatives.next_due_date' in 'where clause'
Problem
I want to get cooperatives with shares that has due dates that are not of cooperative's next_due_date.
cooperatives table columns looks like this: { id,name,next_due_date }-> hasMany(Share)
shares table columns looks like this: { id,cooperative_id } -> hasMany(ShareDue)
share_dues table columns looks like this: { id,cooperative_share_id }->belongsTo(Share)
That lastDue is the shareDue from all shares, it's just hasOne(ShareDue) with latest instead of hasMany(ShareDue)