Skip to content

Commit

Permalink
Merge branch '3.4' into 3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
crynobone committed Oct 30, 2017
2 parents 0468b4b + ee3da2b commit c5ec11a
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions src/Support/Traits/QueryFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Orchestra\Support\Str;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\Expression;

trait QueryFilter
{
Expand Down Expand Up @@ -113,41 +114,61 @@ protected function getBasicQueryOrderBy(array $input)
* @param array $keyword
* @param string $group
*
* @return void
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder
*/
protected function buildWildcardQueryFilters($query, array $fields, array $keyword = [])
{
foreach ($fields as $field) {
if (Str::contains($field, '.') && $query instanceof Builder) {
list($relation, $field) = explode('.', $field, 2);

$query->orWhereHas($relation, function ($query) use ($field, $keyword) {
$this->buildWildcardQueryFilterWithKeyword($query, $field, $keyword, 'where');
});
} else {
$this->buildWildcardQueryFilterWithKeyword($query, $field, $keyword, 'orWhere');
}
$this->buildWildcardForField($query, $field, $keyword);
}

return $query;
}

/**
*
* Build wildcard query filter for field using where or orWhere.
*
* @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder $query
* @param string $field
* @param array $keyword
*
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder
*/
protected function buildWildcardForField($query, $field, array $keyword)
{
if ($field instanceof Expression) {
return $this->buildWildcardForFieldUsing($query, $field->getValue(), $keyword, 'orWhere');
} elseif (! (Str::contains($field, '.') && $query instanceof Builder)) {
return $this->buildWildcardForFieldUsing($query, $field, $keyword, 'orWhere');
}

$this->buildWildcardForFieldUsing($query, $field, $keyword, 'orWhere');
list($relation, $field) = explode('.', $field, 2);

return $query->orWhereHas($relation, function ($query) use ($field, $keyword) {
$this->buildWildcardForFieldUsing($query, $field, $keyword, 'where');
});
}

/**
* Build wildcard query filter by keyword.
* Build wildcard query filter for field using where or orWhere.
*
* @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder $query
* @param string $field
* @param array $keyword
* @param string $group
*
* @return void
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder
*/
protected function buildWildcardQueryFilterWithKeyword($query, $field, array $keyword = [], $group = 'where')
protected function buildWildcardForFieldUsing($query, $field, array $keyword = [], $group = 'where')
{
$callback = function ($query) use ($field, $keyword) {
foreach ($keyword as $key) {
$query->orWhere($field, 'LIKE', $key);
}
};

$query->{$group}($callback);
return $query->{$group}($callback);
}
}

0 comments on commit c5ec11a

Please sign in to comment.