예제 #1
0
 /**
  * Add the restore extension to the builder.
  *
  * @param  \Illuminate\Database\Eloquent\Builder  $builder
  * @return void
  */
 protected function addRestore(Builder $builder)
 {
     $builder->macro('restore', function (Builder $builder) {
         $builder->withTrashed();
         return $builder->update(array($builder->getModel()->getDeletedAtColumn() => null));
     });
 }
예제 #2
0
 /**
  * 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()->whereNotNull($model->getQualifiedDeletedAtColumn());
         return $builder;
     });
 }
예제 #3
0
 /**
  * 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();
         $builder->withoutGlobalScope($this)->whereNotNull($model->getQualifiedDeletedAtColumn());
         return $builder;
     });
 }
예제 #4
0
 /**
  * Apply criteria in query
  *
  * @param Builder $query
  *
  * @return mixed
  */
 public function apply($query, RepositoryInterface $repository = null)
 {
     if ($repository) {
         $this->searchables = array_merge($this->searchables, $repository->getFieldsSearchable());
     }
     if ($nameSearchable = config('repository.criteria.params.search')) {
         $this->setNameSearchable($nameSearchable);
     }
     if ($orderBy = config('repository.criteria.params.orderBy')) {
         $this->setFieldOrderBy($orderBy);
     }
     if ($sortedBy = config('repository.criteria.params.sortedBy')) {
         $this->setFieldSortedBy($sortedBy);
     }
     $model = $query;
     if ($query instanceof EloquentBuilder) {
         $model = $query->getModel();
     }
     if ($model instanceof Model) {
         $this->dates = $model->getDates();
         $this->table = $model->getTable() . '.';
     }
     if ($model instanceof AbstractEntity) {
         $this->addColumns($model->columns());
     }
     foreach ($this->input as $key => $value) {
         // Parameter Grouping
         if ($value instanceof \Closure) {
             $query = $query->where($value);
             continue;
         }
         // Scope
         $methodScope = 'scope' . studly_case($key);
         if (is_object($model) && method_exists($model, $methodScope)) {
             $methodName = camel_case($key);
             $query = $query->{$methodName}($value);
             continue;
         }
         // Where Search
         if ($key === $this->nameSearchable) {
             $query = $this->whereSearch($query, $value);
             continue;
         }
         if (is_int($key)) {
             // Using A Raw Expression
             if ($value instanceof QueryExpression) {
                 $query = $query->whereRaw($value);
             }
             /**
              * Using String Format
              * eg: {field},{operator},{value}
              */
             if (is_string($value) && preg_match('/^([a-zA-Z0-9_]+),(.+),(.+)$/', $value, $matches)) {
                 if (count($matches) == 4) {
                     $value = array_splice($matches, 1, 3);
                 }
             }
             /**
              * Using Array com Operator
              * eg: ex: ('field', '=', 'value') or ('field', 'value')
              */
             if (is_array($value) && count($value)) {
                 $value = array_pad($value, 3, null);
                 list($field, $operator, $valor) = array_splice($value, 0, 3);
                 $query = $this->whereCriteria($query, $field, $operator, $valor);
             }
             continue;
         }
         $query = $this->whereCriteria($query, $key, '=', $value);
     }
     // Order By
     if ($this->orderBy && in_array($this->orderBy, $this->columns)) {
         $query = $query->orderBy($this->orderBy, $this->sortedBy);
     }
     return $query;
 }