I am trying to write an Eloquent query for a BelongsToMany relationship and where the results return only one distinct result based on the pivot table. See below for example.
Installer:
Id | Name |
---|---|
1 | Installer 1 |
2 | Installer 2 |
State:
Id | Name |
---|---|
1 | State 1 |
2 | State 2 |
Pivot:
Id | State Id | Installer Id | Baseline | Created At |
---|---|---|---|---|
1 | 1 | 1 | 1.0 | 2023-01-01 |
2 | 1 | 1 | 1.2 | 2023-01-02 |
3 | 2 | 1 | 1.5 | 2023-01-01 |
So I would like to run the query $state->installers()->get()
where, using the example above, Installer 1 only returns ID 2 and 3. The reason I do not want ID 1 is because it is an older record by the created at column. Is the only way I can accomplish this is with a join? Not eloquent ORM?
2
Answers
you can apply a subquery to load the latest distinct installers for a state.
for example like below
$states->installers will contain the latest distinct installer records for the specified state.
I understand your question and relationship in Pivot Table and I found this solution:
Try this raw
SQL join
with Eloquent.This query will returns the data by
$stateId
where it is firstly check the condition for$stateId
and get the data in orderBycreated_date
field withdistinct
value.I hope this will work for you.