php - Query chaining without using QueryScopes in Laravel -
i'm building analytics dashboard large laravel app contains many complex queries. want chain queries together, otherwise i'm duplicating code on place, write this:
$query_result = $this->customers ->whereactivebetween($dates) ->withordersizegreaterthan($amount) ->get();
because specific, long, , not used in other parts of app, want avoid polluting complex models query scopes going used analytics repository. so, best way achieve this, both keeping code readable, , making code reusable?
have @ traits - feature introduced in php 5.4 allows reuse code in independent classes. can find more details in docs here http://php.net/manual/en/language.oop5.traits.php
in case should help:
// move reusable scopes trait trait analyticsscopes { function whereactivebetween() { code here } function withordersizegreaterthan() { code here } } // add trait model classes class customer extends model { use analyticsscopes; } // use scopes if implemented in models $query_result = $this->customers ->whereactivebetween($dates) ->withordersizegreaterthan($amount) ->get();
Comments
Post a Comment