spatie / laravel-query-builder

Easily build Eloquent queries from API requests

Home Page:https://spatie.be/docs/laravel-query-builder

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Allowed sort for allowed includes

Yeenot opened this issue · comments

How to sort relationship records specified on allowed includes through request sort parameter? I mean, I am aware that it can be put statically on the declaration of relationship in the model but it does not give the user of the api an option to modify the relationship records.

Currently this package doesn't offer an easy, out of the box way to do this. It's easy for one to one relationships (e.g. sort by $user->profile->id) but it gets funky when having to sort with a one to many relationship (e.g. sorting by $user->posts created_at field: what Post model will be used as the sorting value?).

TL;DR: it's not in the package by default but you can create a custom sort class to do this: https://spatie.be/docs/laravel-query-builder/v3/features/sorting#custom-sorts

Good luck!