/**
  * Define your route model bindings, pattern filters, etc.
  */
 public function boot()
 {
     $me = $this;
     $parser = $this->parser;
     /*
      * Search through any columns with score relevance.
      *
      * @param  array|string $keywords
      * @param  array $columns
      * @param  string $groupBy
      * @param  boolean $fulltext
      * @param  float $threshold
      * @return $this
      */
     Builder::macro('search', function ($keywords, array $columns, $fulltext = true, $threshold = null, $groupBy = 'id') use($me, $parser) {
         $words = is_array($keywords) ? $keywords : $parser->parseQuery($keywords, $fulltext);
         $columns = $parser->parseWeights($columns);
         if (count($words) && count($columns)) {
             // Macro is scoped for Query\Builder, so let's trick it by calling
             // in a closure bound to this Searchable class. This allows us
             // to leave all the implementation methods below protected.
             $closure = function () use($me, $words, $columns, $groupBy, $threshold) {
                 $me->query = $this;
                 $me->buildSubquery($words, $columns, $groupBy, $threshold);
                 $me->query = null;
             };
             call_user_func($closure->bindTo($this, get_class($me)));
             return Query::copy($this)->setThreshold($me->threshold);
         }
         return $this;
     });
 }
 /**
  * {@inheritdoc}
  */
 public function register()
 {
     Builder::macro("orderByRandom", function () {
         $randomFunctions = ["mysql" => "RAND()", "pgsql" => "RANDOM()", "sqlite" => "RANDOM()", "sqlsrv" => "NEWID()"];
         $driver = $this->getConnection()->getDriverName();
         return $this->orderByRaw($randomFunctions[$driver]);
     });
 }
 public function boot()
 {
     Builder::macro('if', function ($condition, $column, $operator, $value) {
         if ($condition) {
             return $this->where($column, $operator, $value);
         }
         return $this;
     });
 }
 /**
  * Register the service provider.
  */
 public function register()
 {
     $app = $this->app;
     QueryBuilder::macro('remember', function ($duration, $key = null) use($app) {
         return (new CacheDecorator($this, $app->make('cache.store')))->remember($duration, $key);
     });
     QueryBuilder::macro('rememberForever', function ($key = null) use($app) {
         return (new CacheDecorator($this, $app->make('cache.store')))->rememberForever($key);
     });
 }
Example #5
0
 /**
  * Registry a macro for Builder
  * 
  * */
 protected function registryMacros()
 {
     $sorter = $this->app[Sorter::class];
     Builder::macro('orderBySorter', function (array $whiteList = []) use($sorter) {
         $field = $sorter->getCurrentField();
         if (!$field) {
             return $this;
         }
         if (!$sorter->checkCurrentByWhitelist($whiteList)) {
             $message = "Field '{$field}' is not defined in whitelist";
             throw new \UnexpectedValueException($message);
         }
         $this->orderBy($field, $sorter->getDirection());
         return $this;
     });
 }
Example #6
0
 /**
  * Register handy helper macros.
  *
  * @param  \Illuminate\Database\Query\Builder $query
  * @return void
  */
 protected function registerHelpers()
 {
     Builder::macro('thisYear', function ($column = null) {
         return $this->thisPeriod('year', $column);
     });
     Builder::macro('thisMonth', function ($column = null) {
         return $this->thisPeriod('month', $column);
     });
     Builder::macro('thisWeek', function ($column = null) {
         return $this->thisPeriod('week', $column);
     });
     Builder::macro('today', function ($column = null) {
         return $this->thisPeriod('day', $column);
     });
     Builder::macro('thisHour', function ($column = null) {
         return $this->thisPeriod('hour', $column);
     });
     Builder::macro('thisMinute', function ($column = null) {
         return $this->thisPeriod('minute', $column);
     });
     Builder::macro('nextYear', function ($column = null) {
         return $this->nextPeriod('year', $column);
     });
     Builder::macro('nextMonth', function ($column = null) {
         return $this->nextPeriod('month', $column);
     });
     Builder::macro('nextWeek', function ($column = null) {
         return $this->nextPeriod('week', $column);
     });
     Builder::macro('tomorrow', function ($column = null) {
         return $this->nextPeriod('day', $column);
     });
     Builder::macro('nextHour', function ($column = null) {
         return $this->nextPeriod('hour', $column);
     });
     Builder::macro('nextMinute', function ($column = null) {
         return $this->nextPeriod('minute', $column);
     });
     Builder::macro('lastYear', function ($column = null) {
         return $this->lastPeriod('year', $column);
     });
     Builder::macro('lastMonth', function ($column = null) {
         return $this->lastPeriod('month', $column);
     });
     Builder::macro('lastWeek', function ($column = null) {
         return $this->lastPeriod('week', $column);
     });
     Builder::macro('yesterday', function ($column = null) {
         return $this->lastPeriod('day', $column);
     });
     Builder::macro('lastHour', function ($column = null) {
         return $this->lastPeriod('hour', $column);
     });
     Builder::macro('lastMinute', function ($column = null) {
         return $this->lastPeriod('minute', $column);
     });
     Builder::macro('nextPeriod', function ($unit, $column = null) {
         return $this->periods($unit, 1, $column, false);
     });
     Builder::macro('thisPeriod', function ($unit, $column = null) {
         return $this->periods($unit, 0, $column, true);
     });
     Builder::macro('lastPeriod', function ($unit, $column = null) {
         return $this->periods($unit, -1, $column, false);
     });
 }