macro() public method

Extend the builder with a given callback.
public macro ( string $name, Closure $callback ) : void
$name string
$callback Closure
return void
Esempio n. 1
0
 public function newEloquentBuilder($query)
 {
     $builder = new Builder($query);
     $builder->macro('key', function (Builder $builder) {
         return $builder->getQuery()->key();
     });
     $builder->macro('flush', function (Builder $builder) {
         return $builder->getQuery()->flush();
     });
     return $builder;
 }
Esempio n. 2
0
 /**
  * 使用方法withTrashedUseTo来移除apply添加的条件
  * @param \Illuminate\Database\Eloquent\Builder $builder
  * @return void
  */
 protected function addWithTrashed(Builder $builder)
 {
     $builder->macro('withTrashedUseTo', function (Builder $builder) {
         $this->remove($builder, $builder->getModel());
         return $builder;
     });
 }
Esempio n. 3
0
 /**
  * Extend Builder with custom method.
  *
  * @param \Illuminate\Database\Eloquent\Builder  $builder
  */
 protected function addAllTypes(Builder $builder)
 {
     $builder->macro('allTypes', function (Builder $builder) {
         $this->remove($builder);
         return $builder;
     });
 }
 protected function addWithDrafts(Builder $builder)
 {
     $builder->macro('withDrafts', function (Builder $builder) {
         $this->remove($builder, $builder->getModel());
         return $builder;
     });
 }
 /**
  * Extend Builder with custom method
  *
  * @param \Illuminate\Database\Eloquent\Builder $builder
  */
 protected function addUnordered(Builder $builder)
 {
     $builder->macro('unordered', function (Builder $builder) {
         $this->remove($builder, $builder->getModel());
         return $builder;
     });
 }
 /**
  * Apply the scope to a given Eloquent query builder.
  *
  * @param  \Illuminate\Database\Eloquent\Builder  $builder
  * @return void
  */
 public function apply(Builder $builder)
 {
     $model = $builder->getModel();
     $builder->whereNotNull($model->getQualifiedActivatedAtColumn());
     $builder->macro('withDeactivated', function (Builder $builder) {
         $this->remove($builder);
         return $builder;
     });
     $builder->macro('onlyDeactivated', function (Builder $builder) {
         $model = $builder->getModel();
         $this->remove($builder);
         $builder->getQuery()->whereNotNull($model->getQualifiedActivatedAtColumn());
         return $builder;
     });
     $builder->macro('deactivate', function (Builder $builder) {
         $model = $builder->getModel();
         $builder->withDeactivated();
         return $builder->update([$model->getActivatedAtColumn() => null]);
     });
     $builder->macro('activate', function (Builder $builder) {
         $model = $this->getModel();
         $query = $model->newQuery()->where($model->getKeyName(), $model->getKey());
         $model->{$model->getActivatedAtColumn()} = $time = $model->freshTimestamp();
         $query->update([$model->getActivatedAtColumn() => $model->fromDateTime($time)]);
     });
 }
Esempio n. 7
0
 protected function addOnlyRegistered(Builder $builder)
 {
     $builder->macro('onlyRegistered', function (Builder $builder) {
         $this->remove($builder, $builder->getModel());
         $builder->registered();
         return $builder;
     });
 }
Esempio n. 8
0
 /**
  * Add the only-trashed extension to the builder.
  *
  * @param  \Illuminate\Database\Eloquent\Builder $builder
  * @return void
  */
 protected function addOnlyOffline(Builder $builder)
 {
     $builder->macro('onlyOffline', function (Builder $builder) {
         $model = $builder->getModel();
         $builder->withoutGlobalScope($this)->where($model->getQualifiedStatusColumn(), '=', false);
         return $builder;
     });
 }
Esempio n. 9
0
 protected function addOnlyRegistered(Builder $builder)
 {
     $builder->macro('onlyRegistered', function (Builder $builder) {
         $this->remove($builder, $builder->getModel());
         $builder->where('type', 'private')->where('status', 'publish');
         return $builder;
     });
 }
Esempio n. 10
0
 /**
  * Add the onlyDrafts extension to the builder.
  *
  * @param \Illuminate\Database\Eloquent\Builder $builder
  */
 protected function addOnlyDrafts(Builder $builder)
 {
     $builder->macro('onlyDrafts', function (Builder $builder) {
         $this->remove($builder);
         $builder->getQuery()->where($this->getDraftColumn($builder), '1');
         return $builder;
     });
 }
 /**
  * @param Builder $builder
  */
 protected function addOnlyOldVersions(Builder $builder)
 {
     $builder->macro('onlyOldVersions', function (Builder $builder) {
         $model = $builder->getModel();
         $builder->withoutGlobalScope($this)->where($model->getQualifiedIsCurrentVersionColumn(), 0);
         return $builder;
     });
 }
 /**
  * @param Builder $builder
  */
 protected function addOnlyOldVersions(Builder $builder)
 {
     $builder->macro('onlyOldVersions', function (Builder $builder) {
         $model = $builder->getModel();
         $this->remove($builder, $model);
         $builder->getQuery()->where($model->getQualifiedIsCurrentVersionColumn(), 0);
         return $builder;
     });
 }
Esempio n. 13
0
 protected function addOnlyArchived(Builder $builder)
 {
     $builder->macro('onlyArchived', function (Builder $builder) {
         $model = $builder->getModel();
         $this->remove($builder, $model);
         $builder->getQuery()->whereNotNull($model->getQualifiedArchivedAtColumn());
         return $builder;
     });
 }
Esempio n. 14
0
 /**
  * Extend the Eloquent query builder.
  *
  * @param  \Illuminate\Database\Eloquent\Builder  $builder
  * @return void
  */
 protected function extend(BuilderBase $builder)
 {
     $builder->macro('orderBy', function ($builder, $column, $direction = 'asc') {
         if ($this->scopeApplied) {
             $this->remove($builder, $builder->getModel());
         }
         $builder->getQuery()->orderBy($column, $direction);
         return $builder;
     });
 }
Esempio n. 15
0
 protected function addWithDrafts(Builder $builder)
 {
     $builder->macro('withDrafts', function (Builder $builder) {
         $this->remove($builder, $builder->getModel());
         $builder->with(['article' => function ($query) {
             $query->withDrafts();
         }]);
         return $builder;
     });
 }
Esempio n. 16
0
 /**
  * Add the only-trashed extension to the builder.
  *
  * @param  \Illuminate\Database\Eloquent\Builder  $builder
  * @return void
  */
 protected function addOnlyDrafted(Builder $builder)
 {
     $builder->macro('onlyDrafted', function (Builder $builder) {
         $this->remove($builder);
         $column = $builder->getModel()->getQualifiedDeletedAtColumn();
         $builder->getQuery()->where($column, 'drafted');
         return $builder;
     });
 }
Esempio n. 17
0
 /**
  * Add the only-trashed extension to the builder.
  *
  * @param  \Illuminate\Database\Eloquent\Builder $builder
  * @return void
  */
 protected function addOnlyOffline(Builder $builder)
 {
     $builder->macro('onlyOffline', function (Builder $builder) {
         $model = $builder->getModel();
         $this->remove($builder, $model);
         $builder->getQuery()->where($model->getQualifiedStatusColumn(), '=', false);
         return $builder;
     });
 }
 /**
  * Add the only-trashed extension to the builder.
  *
  * @param  \Illuminate\Database\Eloquent\Builder  $builder
  * @return void
  */
 protected function addOnlyTrashed(Builder $builder)
 {
     $builder->macro('onlyTrashed', function (Builder $builder) {
         $model = $builder->getModel();
         $this->remove($builder, $model);
         $builder->getQuery()->where($model->getQualifiedDeletedAtColumn(), 'trash');
         return $builder;
     });
 }
Esempio n. 19
0
 /**
  * Add the only-trashed extension to the builder.
  *
  * @param  \Illuminate\Database\Eloquent\Builder $builder
  *
  * @return void
  */
 protected function addOnlyDrafts(Builder $builder)
 {
     $builder->macro('onlyDrafts', function (Builder $builder) {
         $model = $builder->getModel();
         $this->remove($builder, $model);
         $builder->getQuery()->where($model->getQualifiedDraftColumn(), 0);
         return $builder;
     });
 }
Esempio n. 20
0
 /**
  * Register handy helper macros.
  *
  * @param  \Illuminate\Database\Eloquent\Builder $query
  * @return void
  */
 protected function registerHelpers(Builder $query)
 {
     $query->macro('thisYear', function (Builder $query, $column = null) {
         return $query->thisPeriod('year', $column);
     });
     $query->macro('thisMonth', function (Builder $query, $column = null) {
         return $query->thisPeriod('month', $column);
     });
     $query->macro('thisWeek', function (Builder $query, $column = null) {
         return $query->thisPeriod('week', $column);
     });
     $query->macro('today', function (Builder $query, $column = null) {
         return $query->thisPeriod('day', $column);
     });
     $query->macro('thisHour', function (Builder $query, $column = null) {
         return $query->thisPeriod('hour', $column);
     });
     $query->macro('thisMinute', function (Builder $query, $column = null) {
         return $query->thisPeriod('minute', $column);
     });
     $query->macro('nextYear', function (Builder $query, $column = null) {
         return $query->nextPeriod('year', $column);
     });
     $query->macro('nextMonth', function (Builder $query, $column = null) {
         return $query->nextPeriod('month', $column);
     });
     $query->macro('nextWeek', function (Builder $query, $column = null) {
         return $query->nextPeriod('week', $column);
     });
     $query->macro('tomorrow', function (Builder $query, $column = null) {
         return $query->nextPeriod('day', $column);
     });
     $query->macro('nextHour', function (Builder $query, $column = null) {
         return $query->nextPeriod('hour', $column);
     });
     $query->macro('nextMinute', function (Builder $query, $column = null) {
         return $query->nextPeriod('minute', $column);
     });
     $query->macro('lastYear', function (Builder $query, $column = null) {
         return $query->lastPeriod('year', $column);
     });
     $query->macro('lastMonth', function (Builder $query, $column = null) {
         return $query->lastPeriod('month', $column);
     });
     $query->macro('lastWeek', function (Builder $query, $column = null) {
         return $query->lastPeriod('week', $column);
     });
     $query->macro('yesterday', function (Builder $query, $column = null) {
         return $query->lastPeriod('day', $column);
     });
     $query->macro('lastHour', function (Builder $query, $column = null) {
         return $query->lastPeriod('hour', $column);
     });
     $query->macro('lastMinute', function (Builder $query, $column = null) {
         return $query->lastPeriod('minute', $column);
     });
     $query->macro('nextPeriod', function (Builder $query, $unit, $column = null) {
         return $query->periods($unit, 1, $column, false);
     });
     $query->macro('thisPeriod', function (Builder $query, $unit, $column = null) {
         return $query->periods($unit, 0, $column, true);
     });
     $query->macro('lastPeriod', function (Builder $query, $unit, $column = null) {
         return $query->periods($unit, -1, $column, false);
     });
 }
Esempio n. 21
0
 /**
  * Add the where-tenant extension to the builder.
  * Include an array of specific tenant ids.
  *
  * @param  \Illuminate\Database\Eloquent\Builder  $builder
  * @param  int|array $arg  a single or array of ids
  * @return void
  */
 protected function addApplyTenant(Builder $builder)
 {
     $builder->macro('applyTenant', function (Builder $builder) {
         $this->remove($builder);
         // Position 1 in func_args is from the user input macro
         // parameter. Position 0 is the $builder instance
         $arg = func_get_args()[1];
         if (is_array($arg) && count($arg) <= 1) {
             $arg = $arg[0];
         }
         if (is_null($arg) || empty($arg)) {
             throw new TenantIdNotSetException();
         }
         $where = is_array($arg) ? 'whereIn' : 'where';
         $builder->getQuery()->{$where}($builder->getModel()->getQualifiedTenantColumn(), $arg);
         return $builder;
     });
 }
 /**
  * Add the Reject extension to the builder.
  *
  * @param  \Illuminate\Database\Eloquent\Builder $builder
  *
  * @return void
  */
 protected function addReject(Builder $builder)
 {
     $builder->macro('reject', function (Builder $builder, $id = null) {
         $builder->withAnyStatus();
         //If $id parameter is passed then update the specified model
         if ($id) {
             $model = $builder->find($id);
             $model->{$model->getStatusColumn()} = Status::REJECTED;
             $model->{$model->getModeratedAtColumn()} = Carbon::now();
             return $model->save();
         }
         return $builder->update([$builder->getModel()->getStatusColumn() => Status::REJECTED, $builder->getModel()->getModeratedAtColumn() => Carbon::now()]);
     });
 }
Esempio n. 23
0
 /**
  * Extend the builder.
  * @param Builder $builder
  */
 public function extend(EloquentBuilder $builder)
 {
     $builder->macro('onlyTranslated', function (EloquentBuilder $builder, $locale = null) {
         $builder->getModel()->setOnlyTranslated(true);
         if ($locale) {
             $builder->getModel()->setLocale($locale);
         }
         return $builder;
     });
     $builder->macro('withUntranslated', function (EloquentBuilder $builder) {
         $builder->getModel()->setOnlyTranslated(false);
         return $builder;
     });
     $builder->macro('withFallback', function (EloquentBuilder $builder, $fallbackLocale = null) {
         $builder->getModel()->setWithFallback(true);
         if ($fallbackLocale) {
             $builder->getModel()->setFallbackLocale($fallbackLocale);
         }
         return $builder;
     });
     $builder->macro('withoutFallback', function (EloquentBuilder $builder) {
         $builder->getModel()->setWithFallback(false);
         return $builder;
     });
     $builder->macro('translateInto', function (EloquentBuilder $builder, $locale) {
         if ($locale) {
             $builder->getModel()->setLocale($locale);
         }
         return $builder;
     });
     $builder->macro('withoutTranslations', function (EloquentBuilder $builder) {
         $builder->withoutGlobalScope(static::class);
         return $builder;
     });
     $builder->macro('withAllTranslations', function (EloquentBuilder $builder) {
         $builder->withoutGlobalScope(static::class)->with('translations');
         return $builder;
     });
 }
 /**
  * Add the with-trashed extension to the builder.
  *
  * @param  \Illuminate\Database\Eloquent\Builder $builder
  * @return void
  */
 protected function addWithoutTranslation(Builder $builder)
 {
     $builder->macro('withoutTranslation', function (Builder $builder) {
         $this->remove($builder, $builder->getModel());
         return $builder;
     });
 }
Esempio n. 25
0
 /**
  * Extend Builder with custom method.
  *
  * @param \Illuminate\Database\Eloquent\Builder $builder
  */
 protected function addAllApplications(Builder $builder)
 {
     $builder->macro('allApplications', function (Builder $builder) {
         $this->remove($builder, $builder->getModel());
         return $builder;
     });
 }
Esempio n. 26
0
 /**
  * Extend Builder with custom method.
  *
  * @param \Illuminate\Database\Eloquent\Builder $builder
  */
 protected function allLanguages(Builder $builder)
 {
     $builder->macro('withAllLanguages', function (Builder $builder) {
         $this->remove($builder, $builder->getModel());
         return $builder;
     });
 }
 /**
  * Extend Builder with custom method
  *
  * @param \Illuminate\Database\Eloquent\Builder $builder
  */
 protected function addForAnyOrganization(Builder $builder)
 {
     $builder->macro('forAnyOrganization', function (Builder $builder) {
         $this->remove($builder, $builder->getModel());
         return $builder;
     });
 }
Esempio n. 28
0
 /**
  * Extend Builder with custom method.
  *
  * @param \Illuminate\Database\Eloquent\Builder $builder
  */
 protected function addSiteAgnostic(Builder $builder)
 {
     $builder->macro('siteAgnostic', function (Builder $builder) {
         $this->remove($builder, $builder->getModel());
         return $builder;
     });
 }
Esempio n. 29
0
 /**
  * Extend the builder with a given callback.
  *
  * @param string $name
  * @param \Closure $callback
  * @return void 
  * @static 
  */
 public static function macro($name, $callback)
 {
     \Illuminate\Database\Eloquent\Builder::macro($name, $callback);
 }
 /**
  * Add the allVersions extension to the builder.
  *
  * @param  \Illuminate\Database\Eloquent\Builder  $builder
  * @return void
  */
 protected function addAllVersions(Builder $builder)
 {
     $builder->macro('allVersions', function (Builder $builder) {
         $model = $builder->getModel();
         $this->remove($builder, $builder->getModel());
         $builder->join($model->getVersionTable(), function ($join) use($model) {
             $join->on($model->getQualifiedKeyName(), '=', $model->getQualifiedVersionKeyName());
         });
         return $builder;
     });
 }