Create a one-of-many intervalAmount relation on the PartnerRateType model rather than using first in the eager-loading constraint.
Trouble retrieving a relationship in a query
Hello everyone, I'm a bit stuck on an Eloquent request that's giving me a bit of trouble. The request concerns an application that manages loan applications for banking partners. A banking partner has rates, price intervals and interest rates per month for these intervals.
Here are my tables:
partner_rate_types :
partner_id, rate_type_id
interval_amounts :
partner_rate_type_id, amount_min, amount_max
interval_duration_percentages :
interval_amount_id, duration_interval, pourcentage
- PartnerRateTypes can have several intervalAmounts
- IntervalAmounts can have several intervalDurationPercentages The relationships are in place and working.
Now, here's the Eloquent query I made:
PartnerRateType::where('rate_type_id', $rateTypeId)
->with([
'intervalAmounts' => function ($query) use ($amount) {
$query->where('amount_min', '<=', $amount)
->where('amount_max', '>=', $amount)
->orderBy('amount_min', 'desc')
->first()
->load('intervalDurationPercentages');
}
])
->get();
Here the result, everything is okay for the first partner : https://i.postimg.cc/6p5dNMRL/tt.png
But for the second, the relationship is empty: https://i.postimg.cc/SRSKzvmg/tt2.png
If I manually select the second partner with a different query but load the relationship in the same way, it works:
PartnerRateType::where('rate_type_id', $rateTypeId)
->where('id', 2)
->with([
'intervalAmounts' => function ($query) use ($amount) {
$query->where('amount_min', '<=', $amount)
->where('amount_max', '>=', $amount)
->orderBy('amount_min', 'desc')
->first()
->load('intervalDurationPercentages');
}
])
->get();
As you can see with the results : https://i.postimg.cc/8Nw4ZvLB/tt3.png
My parameters are exactly the same for the 2 requests. Namely:
$amount = 3701.00
$rateTypeId = 11
So why aren't the results the same? Can anyone help me with this? Thanks in advance
Please or to participate in this conversation.