本文整理汇总了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;
});
}
示例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]);
});
}
示例3: boot
public function boot()
{
Builder::macro('if', function ($condition, $column, $operator, $value) {
if ($condition) {
return $this->where($column, $operator, $value);
}
return $this;
});
}
示例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);
});
}
示例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;
});
}
示例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);
});
}