Skip to content

Commit

Permalink
Recursive relations on SearchCriteria
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrián Pardellas Blunier committed Oct 3, 2016
1 parent 8b87771 commit 997b9aa
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/Repository/Criteria/SearchCriteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,27 @@ private function setRelationFieldCondition($query, $column, $or = true)
{
$columnRelation = explode('.', $column);

$firstRelation = array_shift($columnRelation);

if ($or) {
$query->orWhereHas($columnRelation[0], function ($subquery) use ($columnRelation) {
$subquery->where($columnRelation[1], 'LIKE', '%'.$this->queryString.'%');
});
$query->orWhereHas($firstRelation, $this->getRelationClosure($columnRelation));
} else {
$query->whereHas($columnRelation[0], function ($subquery) use ($columnRelation) {
$subquery->where($columnRelation[1], 'LIKE', '%'.$this->queryString.'%');
});
$query->whereHas($firstRelation, $this->getRelationClosure($columnRelation));
}

return $query;
}

private function getRelationClosure(array $relations)
{
return function ($query) use ($relations) {
$relation = array_shift($relations);

if (count($relations) > 0) {
$query->whereHas($relation, $this->getRelationClosure($relations));
} else {
$query->where($relation, 'LIKE', '%'.$this->queryString.'%');
}
};
}
}

0 comments on commit 997b9aa

Please sign in to comment.