-
Notifications
You must be signed in to change notification settings - Fork 107
Control count of included resources when querying the relation of an endpoint #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi @jstoone What you are trying to do isn't supported by the To be fair to that encoding package, what you are trying to do isn't mentioned in the JSON API spec (as far as I can see). Under the section on the My suggestion (though I'm obviously not familiar with your use case and client/server set up) would be to user the following: Add a
On your public function stars()
{
return $this->hasMany(Star::class);
}
public function topStars()
{
return $this->stars()->orderBy('created_at', 'desc')->limit(5);
} To be honest, if your users can have lots of stars, it's probably not worth having a JSON API relationship in the What you'd actually do is instead of returning To return a link instead: return [
'top-stars' => [self::DATA => $resource->topStars],
'stars' => [self::SHOW_RELATED => true]
]; As per: https://github.com/neomerx/json-api/wiki/Schemas Hope this makes some sort of sense! |
Uff nice reponse, and that was actually what we ended up trying. I'll close this issue for now since as you mention nothing that is natively supported by the spec, and therefore not relevant to the package itself. 👍 |
great, yep keep me in the loop with the laravel bug. |
@lindyhopchris as promised: laravel/framework#18014 |
Uh oh!
There was an error while loading. Please reload this page.
Story time
When requesting the relation of an endpoint i.e:
/api/v1/companies/1/users
i get a list of users within the company of ID 1, true.Now let's say I want to include some "likes" for each user, so we add that as a query parameter:
/app/v1/companies/1/users?include=stars
. Now we get a list of users, and all their stars have been included in the response as well, true.Problem time
Let's say I want to be more specific, and only get 5 stars per user included. How would I go around solving this?
First try
In the App\Models\User eloquent model of mine i just cold heartedly added a limit onto the query:
But this resulted in there only being returned 5 stars in total.
Where to go next
I am trying to dig deeper how loading of included models works, but I am having a hard time navigating around and finding the source where the final query is constructed.
It does seem a bit naughty to me that the stars relationship shares its limit globally, since it should only limit the amount of stars for that one user instance, right? I would understand if the relation method was static and therefore would apply to the entire collection of users.
Am I missing something, a filter or something that actually lets me do exactly this? Looking forward to hearing from you @lindyhopchris :)
The text was updated successfully, but these errors were encountered: