samedi 28 mai 2016

Query chaining without using QueryScopes in Laravel

I'm building an analytics dashboard for a large laravel app that contains many complex queries. Often I want to chain queries together, as otherwise I'm duplicating code all over the place, to write something like this:

$query_result = $this->customers
    ->whereActiveBetween($dates)
    ->withOrdersizeGreaterThan($amount)
    ->get();

Because they are very specific, long, and not used in other parts of the app, I want to avoid polluting my already complex models with query scopes that are only going to be used by the analytics repository. So, what is the best way to achieve this, both keeping code readable, and making the code reusable?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire