Skip to content

Commit

Permalink
Fix multiple relation search
Browse files Browse the repository at this point in the history
  • Loading branch information
TitasGailius committed Sep 2, 2018
1 parent de329d7 commit c2585e8
Showing 1 changed file with 6 additions and 29 deletions.
35 changes: 6 additions & 29 deletions src/SearchesRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public static function searchable()
*/
protected static function applySearch($query, $search)
{
return tap(parent::applySearch($query, $search), function ($query) use ($search) {
static::applyRelationSearch($query, $search);
return $query->where(function ($query) use ($search) {
return static::applyRelationSearch(parent::applySearch($query, $search), $search);
});
}

Expand All @@ -36,23 +36,17 @@ protected static function applySearch($query, $search)
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $search
* @return void
* @return \Illuminate\Database\Eloquent\Builder
*/
public static function applyRelationSearch(Builder $query, string $search)
public static function applyRelationSearch(Builder $query, string $search): Builder
{
foreach (static::searchableRelations() as $relation => $columns) {
foreach ($searchableRelations = static::searchableRelations() as $relation => $columns) {
$query->orWhereHas($relation, function ($query) use ($columns, $search) {
$query->where(static::searchQueryApplier($columns, $search));
});
}

// All search conditionals have to be combined into 1 nested conditional.
// We just applied a query to search for relationship columns while Nova
// has also applied a separate query to search for model columns. That's
// why we merge last 2 conditionals into 1 nested conditional which leaves
// us with this nicely formatted query that also seatches in relationships.
// SELECT ? FROM ? WHERE ($searchQuery OR $relationQuery)
static::mergeLastWheres($query, 2);
return $query;
}

/**
Expand Down Expand Up @@ -80,21 +74,4 @@ public static function searchQueryApplier(array $columns, string $search): Closu
}
};
}

/**
* Merge last where conditionals into a 1 nested conditional.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param int $count
* @return void
*/
public static function mergeLastWheres(Builder $query, int $count)
{
$query = $query->getQuery();
$queries = array_splice($query->wheres, $count);

$query->where(function ($query) use ($queries) {
$query->wheres = array_merge($query->wheres, $queries);
});
}
}

0 comments on commit c2585e8

Please sign in to comment.