From f0147798ed448bc698623f5301733cb457029228 Mon Sep 17 00:00:00 2001 From: Yogesh Sharma Date: Mon, 7 Sep 2020 13:04:11 +0530 Subject: [PATCH] added support for where in clause --- composer.json | 2 +- src/DataTable.php | 29 ++++++++++++++++++++++++++++- src/Request.php | 11 ++++++++++- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 031195f..9b2b85b 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "package to help in handling server side", "version": "1.0", "keywords": [ - "iYogesharrma", + "iYogesharma", "element-ui", "vue-datatable", "datatable", diff --git a/src/DataTable.php b/src/DataTable.php index d06ea00..0a0b0a2 100644 --- a/src/DataTable.php +++ b/src/DataTable.php @@ -199,7 +199,34 @@ protected function setTotalData() */ protected function setFilters() { - $this->query = $this->query->where($this->request->getFilters()); + $filters = $this->request->getFilters(); + $this->query = $this->query->where($filters['basic']); + if( count($filters['array']) > 0 ) + { + $this->setArrayFilters( $filters['array']); + } + + } + + /** + * set array filter conditions on query + * + * @param array $filters + */ + protected function setArrayFilters( array $filters ) + { + foreach($filters as $k => $v ) + { + if( strpos($k,'_at') !== true || strpos($k,'date') !== true || strpos($k,'time') !== true) + { + $this->query = $this->query->whereBetween($k,$v); + } + else + { + $this->query = $this->query->whereIn($k,$v); + } + + } } /** diff --git a/src/Request.php b/src/Request.php index 83243e0..ced5b2b 100644 --- a/src/Request.php +++ b/src/Request.php @@ -99,6 +99,15 @@ public function hasFilters() * @return array */ public function getFilters(){ - return json_decode($this->request->input('filters'),true) ; + $filters = json_decode($this->request->input('filters'),true) ; + $arrayFilters = []; + foreach($filters as $k=>$v) { + if(gettype($v) === 'array') { + $arrayFilters[$k] = $v; + unset($filters[$k]); + } + } + + return [ 'basic' => $filters, 'array' => $arrayFilters ]; } }