Skip to content

Filtering & Sorting by Model append Attributes #459

Answered by AlexVanderbist
shingsoso asked this question in Q&A
Discussion options

You must be logged in to vote

Hi! What @ashgibson says is true. The query builder builds Eloquent queries, it doesn't modify collections. Adding support for appended models might've been a mistake from the beginning 😅

I'd actually recommend looking into SQL and Eloquent queries more. Most of the extra attributes I add to models can actually be written as selects/joins/relations/etc.

For your total review count example we actually support selecting and sorting by relationship count:

$users = QueryBuilder::for(User::class)
    ->allowedIncludes([
        'posts', // allows including `posts` or `postsCount`
    ])
    ->allowedSorts('posts_count'); 

Good luck!

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by AlexVanderbist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #459 on June 17, 2020 07:09.