Skip to content

Commit

Permalink
Add in condition (#743)
Browse files Browse the repository at this point in the history
* Update repository.php

* added In criteria

* Update RequestCriteria.php

* revert

* Add in condition to requestCriteria (#741) (#742)

* Update repository.php

* added In criteria

* Update RequestCriteria.php
  • Loading branch information
bsormagec authored Dec 20, 2020
1 parent f2a4b23 commit ecb9a5d
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions 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)) {
if (!is_null($search) && $condition !== 'in') {
$value = ($condition == "like" || $condition == "ilike") ? "%{$search}%" : $search;
}
}
Expand All @@ -85,20 +85,26 @@ public function apply($model, RepositoryInterface $repository)
$field = array_pop($explode);
$relation = implode('.', $explode);
}
if($condition === 'in'){
$value = explode(',',$value);
if( trim($value[0]) === "" || $field == $value[0]){
$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,explode(',',$value));
$query->whereIn($field,$value);
}else{
$query->where($field,$condition,$value);
}
});
} else {
if($condition === 'in'){
$query->whereIn($modelTableName.'.'.$field,explode(',',$value));
$query->whereIn($modelTableName.'.'.$field,$value);
}else{
$query->where($modelTableName.'.'.$field,$condition,$value);
}
Expand All @@ -110,14 +116,14 @@ public function apply($model, RepositoryInterface $repository)
if(!is_null($relation)) {
$query->orWhereHas($relation, function($query) use($field,$condition,$value) {
if($condition === 'in'){
$query->whereIn($field,explode(',',$value));
$query->whereIn($field,$value);
}else{
$query->where($field,$condition,$value);
}
});
} else {
if($condition === 'in'){
$query->orWhereIn($modelTableName.'.'.$field, explode(',',$value));
$query->orWhereIn($modelTableName.'.'.$field, $value);
}else{
$query->orWhere($modelTableName.'.'.$field, $condition, $value);
}
Expand Down

0 comments on commit ecb9a5d

Please sign in to comment.