Skip to content

Commit ecf488f

Browse files
committed
add the possibility to use a makeHidden() on an Eloquent Object
1 parent 177b436 commit ecf488f

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

src/DataTableAbstract.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,20 @@ public function escapeColumns($columns = '*')
246246
return $this;
247247
}
248248

249+
/**
250+
* Add a makeHidden() to the row object
251+
*
252+
* @param array $attributes
253+
* @return $this
254+
*/
255+
public function makeHidden(array $attributes = [])
256+
{
257+
258+
$this->columnDef['make_hidden'] = array_merge_recursive($this->columnDef['make_hidden'] ?: [], $attributes);
259+
260+
return $this;
261+
}
262+
249263
/**
250264
* Set columns that should not be escaped.
251265
* Optionally merge the defaults from config.

src/Processors/DataProcessor.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public function __construct($results, array $columnDef, array $templates, $start
8282
$this->escapeColumns = $columnDef['escape'];
8383
$this->includeIndex = $columnDef['index'];
8484
$this->rawColumns = $columnDef['raw'];
85+
$this->makeHidden = $columnDef['make_hidden'];
8586
$this->templates = $templates;
8687
$this->start = $start;
8788
}
@@ -98,7 +99,7 @@ public function process($object = false)
9899
$indexColumn = config('datatables.index_column', 'DT_RowIndex');
99100

100101
foreach ($this->results as $row) {
101-
$data = Helper::convertToArray($row);
102+
$data = Helper::convertToArray($row, ['make_hidden' => $this->makeHidden]);
102103
$value = $this->addColumns($data, $row);
103104
$value = $this->editColumns($value, $row);
104105
$value = $this->setupRowVariables($value, $row);
@@ -179,7 +180,17 @@ protected function selectOnlyNeededColumns(array $data)
179180
if (is_null($this->onlyColumns)) {
180181
return $data;
181182
} else {
182-
return array_intersect_key($data, array_flip(array_merge($this->onlyColumns, $this->exceptions)));
183+
$dotted_array = array_intersect_key(
184+
array_dot($data),
185+
array_flip(array_merge($this->onlyColumns, $this->exceptions))
186+
);
187+
188+
$nested_array = [];
189+
foreach ($dotted_array as $key => $value) {
190+
array_set($nested_array, $key, $value);
191+
}
192+
193+
return $nested_array;
183194
}
184195
}
185196

src/Utilities/Helper.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,12 @@ public static function getOrMethod($method)
151151
* Converts array object values to associative array.
152152
*
153153
* @param mixed $row
154+
* @param array $filters
154155
* @return array
155156
*/
156-
public static function convertToArray($row)
157+
public static function convertToArray($row, $filters = [])
157158
{
159+
$row = method_exists($row, 'makeHidden') ? $row->makeHidden($filters['make_hidden']) : $row;
158160
$data = $row instanceof Arrayable ? $row->toArray() : (array) $row;
159161

160162
foreach ($data as &$value) {

0 commit comments

Comments
 (0)