jesperbjerke / laravel-api-query-builder Goto Github PK
View Code? Open in Web Editor NEWA query builder for Laravel that parses the request and uses Eloquent ORM to query database
License: MIT License
A query builder for Laravel that parses the request and uses Eloquent ORM to query database
License: MIT License
Is there a way to use eager loading with this library?
For example, when selecting a single entity via API, it would be cool to eager load relationships instead or generating a duplicate query:
// controller
public function show(Thing $thing): JsonResponse
{
$thing->loadMissing('relation');
return ThingResource::make($thing)->response();
}
As it is now, I need to do this:
public function show(Thing $thing, Request $request): JsonResponse
{
$thing = (new QueryBuilder($thing, $request))->build()->first();
return ThingResource::make($thing)->response();
}
Seems a bit un-pretty ๐ค
Update:
My example above for showing a single entity didn't work, it always returned the first result of the new query.
This change did get me what I want, but it still uses the same code above in the controller.
// in QueryBuilder::build()
$query = $this->model->exists
? $this->model->newQueryForRestoration($this->model)
: $this->model->newQuery();
Update 2:
Something like this could work pretty well I think, adding an extra method to the trait (same for loadCount
).
// in QueryBuilderModelTrait
public function loadApiRelations(): self
{
$with = collect(request('with'))
->map(fn($item) => explode(',', $item))
->flatten();
return $this->loadMissing($this->validatedApiRelations($with));
}
Then it can be used as:
// controller
public function show(Thing $thing): JsonResponse
{
$thing->loadApiRelations();
return ThingResource::make($thing)->response();
}
I've been trying to filter results by counts (where[things_count]=5
), but am getting Unknown column 'things_count'
, because of the way the query is put together:
select `things`.*, (select count(*) from `things`) as `things_count` from `things` where ((`things_count` = 5))
Playing around a bit, it should be having
instead of where
. I've tried adding the having
to the list of allowed query methods, but that didn't work for me.
Adding a dedicated getHaving
(as with groupBy
and others) it does work though (having[things_count]=5
), as it gives me:
select `things`.*, (select count(*) from `things`) as `things_count` from `things` having `things_count` = 5
I'm just wondering if I'm missing something here and there is actually a way to filter by *_count
fields.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.