Esempio n. 1
0
 public function build(Builder $query)
 {
     $text = $this->title;
     return $query->whereNested(function (QueryBuilder $query) use($text) {
         return $query->where('title', 'like', "%{$text}%")->orWhere('description', 'like', "%{$text}%");
     });
 }
 /**
  * @param \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder $query
  *
  * @return void
  */
 public function apply($query)
 {
     $query->where(function ($query) {
         $logic = $this->logic === static::LOGIC_AND ? 'and' : 'or';
         foreach ($this->criteria as $criteria) {
             $query->whereNested(function ($query) use($criteria) {
                 $criteria->apply($query);
             }, $logic);
         }
     });
 }
 /**
  * @param Builder|QueryBuilder $queryBuilder
  * @param array $wheres
  * @param string $boolean
  */
 protected function buildWheres($queryBuilder, array $wheres, $boolean = 'and')
 {
     foreach ($wheres as $whereField => $where) {
         if (!isset($whereField) || !isset($where)) {
             continue;
         }
         $whereField = strtolower($whereField);
         // Nested OR where
         // Example: 'or' => ['foo' => 'bar', 'x => 'y']
         if ($whereField === 'or') {
             $queryBuilder->whereNested(function ($queryBuilder) use($where) {
                 $this->buildWheres($queryBuilder, $where, 'or');
             }, $boolean);
             continue;
         }
         // Nested AND where
         // Example: 'and' => ['foo' => 'bar', 'x => 'y']
         if ($whereField === 'and') {
             $queryBuilder->whereNested(function ($queryBuilder) use($where) {
                 $this->buildWheres($queryBuilder, $where, 'and');
             }, $boolean);
             continue;
         }
         // Operator is present on query
         // Example: 'foo' => ['like' => '%bar%']
         if (is_array($where)) {
             foreach ($where as $whereOperator => $whereValue) {
                 $whereOperator = $this->parseOperator($whereOperator);
                 $this->buildWhere($queryBuilder, $whereField, $whereOperator, $whereValue, $boolean);
             }
             continue;
         }
         // Operator is omitted on query, assumes '='
         // Example: 'foo' => 'bar'
         $whereOperator = is_array($where) ? array_keys($where)[0] : '=';
         $whereValue = is_array($where) ? $where[$whereOperator] : $where;
         $whereOperator = $this->parseOperator($whereOperator);
         $this->buildWhere($queryBuilder, $whereField, $whereOperator, $whereValue, $boolean);
     }
 }