Skip to content

Commit

Permalink
Between filter (#745)
Browse files Browse the repository at this point in the history
* added between filter

* between filter
  • Loading branch information
bsormagec authored Jan 8, 2021
1 parent a1a2e20 commit d4cd5af
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/Prettus/Repository/Criteria/RequestCriteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function apply($model, RepositoryInterface $repository)
if (isset($searchData[$field])) {
$value = ($condition == "like" || $condition == "ilike") ? "%{$searchData[$field]}%" : $searchData[$field];
} else {
if (!is_null($search) && $condition !== 'in') {
if (!is_null($search) && !in_array($condition,['in','between'])) {
$value = ($condition == "like" || $condition == "ilike") ? "%{$search}%" : $search;
}
}
Expand All @@ -91,20 +91,30 @@ public function apply($model, RepositoryInterface $repository)
$value = null;
}
}
if($condition === 'between'){
$value = explode(',',$value);
if(count($value) < 2){
$value = null;
}
}
$modelTableName = $query->getModel()->getTable();
if ( $isFirstField || $modelForceAndWhere ) {
if (!is_null($value)) {
if(!is_null($relation)) {
$query->whereHas($relation, function($query) use($field,$condition,$value) {
if($condition === 'in'){
$query->whereIn($field,$value);
}elseif($condition === 'between'){
$query->whereBetween($field,$value);
}else{
$query->where($field,$condition,$value);
}
});
} else {
if($condition === 'in'){
$query->whereIn($modelTableName.'.'.$field,$value);
}elseif($condition === 'between'){
$query->whereBetween($modelTableName.'.'.$field,$value);
}else{
$query->where($modelTableName.'.'.$field,$condition,$value);
}
Expand All @@ -117,13 +127,17 @@ public function apply($model, RepositoryInterface $repository)
$query->orWhereHas($relation, function($query) use($field,$condition,$value) {
if($condition === 'in'){
$query->whereIn($field,$value);
}elseif($condition === 'between'){
$query->whereBetween($field, $value);
}else{
$query->where($field,$condition,$value);
}
});
} else {
if($condition === 'in'){
$query->orWhereIn($modelTableName.'.'.$field, $value);
}elseif($condition === 'between'){
$query->whereBetween($modelTableName.'.'.$field,$value);
}else{
$query->orWhere($modelTableName.'.'.$field, $condition, $value);
}
Expand Down

0 comments on commit d4cd5af

Please sign in to comment.