Giter VIP home page Giter VIP logo

laravel-api-query-builder's People

Contributors

jesperbjerke avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

f4bio mosbror

laravel-api-query-builder's Issues

Eager loading data

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();
}

Filtering by counts

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.