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

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -