What am I missing?
Nothing, you are missing nothing.
Unfortunately there are some situations an ORM does not cover the use-case we want. This is true for Eloquent not supporting three-way pivot tables. As I already told you on the other thread:
https://laracasts.com/discuss/channels/eloquent/laravel-9-3-column-pivot-table-nested-json
Which includes a proposed solution to your problem, that you seem not to believe is a good one.
Is this possible within eloquent without multiple queries and manual processing
Unfortunately no.
One thing I already suggested you on the previous thread was to write a custom relation class, extending from the BelongsToMany one, that would add the needed constraint under the hood.
Or to search for a package that already provides such relation.
or is there a magic eloquent one liner that will give me what I'm looking for.
No, there isn't.
If you believe this should be part of the Eloquent ORM you can try sending a PR to Laravel's GitHub repository.
But currently there isn't. Unfortunately an ORM cannot cover all use-cases, and more important, it would be very hard to maintain every use-case.
It seems you want to find something ready-made, by someone else's work, instead of collaborating with the solution to a use-case you are amused Eloquent does not ship with a built-in solution.
If you want a one-liner (actually a one-command) you can use the Model::fromRaw() and use a custom SQL statement:
Locations::with([
'services.documents' => function ($relation) {
// custom SQL that filters documents based
// on the selected services
$relation->fromRaw('SELECT * FROM ...', [/* bindings */]);
},
])->get();
Of course I believe you are not expecting someone to write the inner SQL for you, right?
Epilogue
On the other thread I showed you two solutions, what is wrong with them?
If you don't like the manual processing, or you think it is somehow ugly to have it around, you can move it to either a local scope or a custom collection.
Some good reading for you:
- https://timacdonald.me/giving-collections-a-voice
- https://timacdonald.me/query-scopes-meet-action-scopes
At the time of these post were written, Tim McDonald wasn't a core Laravel team member. As of today, he is.
Or you can try a third thread to see if someone else will come up with an easier, or a more beautiful/pleasing solution.