当前位置: 首页>>代码示例>>PHP>>正文


PHP Builder::macro方法代码示例

本文整理汇总了PHP中Illuminate\Database\Query\Builder::macro方法的典型用法代码示例。如果您正苦于以下问题:PHP Builder::macro方法的具体用法?PHP Builder::macro怎么用?PHP Builder::macro使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Illuminate\Database\Query\Builder的用法示例。


在下文中一共展示了Builder::macro方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: boot

 /**
  * 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;
     });
 }
开发者ID:jarektkaczyk,项目名称:laravel-searchable,代码行数:35,代码来源:Searchable.php

示例2: register

 /**
  * {@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]);
     });
 }
开发者ID:kevinsimard,项目名称:laravel-builder-macros,代码行数:11,代码来源:OrderByRandomServiceProvider.php

示例3: boot

 public function boot()
 {
     Builder::macro('if', function ($condition, $column, $operator, $value) {
         if ($condition) {
             return $this->where($column, $operator, $value);
         }
         return $this;
     });
 }
开发者ID:laugharn,项目名称:macros,代码行数:9,代码来源:BuilderServiceProvider.php

示例4: register

 /**
  * 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);
     });
 }
开发者ID:viniijui,项目名称:codeTags,代码行数:13,代码来源:CachableQueryServiceProvider.php

示例5: registryMacros

 /**
  * 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;
     });
 }
开发者ID:laravellegends,项目名称:sorter,代码行数:20,代码来源:SorterProvider.php

示例6: registerHelpers

 /**
  * 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);
     });
 }
开发者ID:jarektkaczyk,项目名称:laravel-scopes,代码行数:72,代码来源:Periods.php


注:本文中的Illuminate\Database\Query\Builder::macro方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。