Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Lopsum's avatar

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

0 likes
2 replies
tykus's avatar

Create a one-of-many intervalAmount relation on the PartnerRateType model rather than using first in the eager-loading constraint.

1 like
Lopsum's avatar

Thanks for your help :)

But how to use it with the $amount indicated by the user ?

Because I can't just retrieve the first or the last, I need the amount indicated by the user to retrieve the corresponding IntervalAmounts and finally output the first retrieved.

Let's imagine that the user enters "5800.00"

In this case, I need to retrieve this IntervalAmount for partner 1:

#1 :

id => 2,
partner_rate_type_id => 1,
amount_min => 5601.00,
amount_max => 7500.00

The one before must not be retrived because the amount_max is greater than $amount and the next ones, the amount_min is greater than $amount : Ex: for the before one :

id => 1,
partner_rate_type_id => 1,
amount_min => 3701.00,
amount_max => 5600.00

And for another one :

id => 1,
partner_rate_type_id => 1,
amount_min => 10001.00,
amount_max => 15000.00

Please or to participate in this conversation.