Skip to content

How to incorporate Eloquent API resource with QueryBuilder #873

Closed Answered by KnudH
wonder95 asked this question in Q&A
Discussion options

You must be logged in to vote

I'm not sure, if you still need help with this, but will give a try. Overall, it is stupid simple to incorporate both, as the QueryBuilder is doing nothing more than creating the query. So you can just take the query build from QueryBuilder, execute it and give the result to your Eloquent API Resource / Collection.

return \App\Http\Resources\PersonResource::collection(
    QueryBuilder::for(Person::class)
        ->allowedFilters(['first_name', 'last_name'])
        ->allowedSorts(['first_name', 'last_name'])
        ->paginate()
);

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by wonder95
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants