Skip to content

Commit 7512bc4

Browse files
authored
Merge pull request yajra#2085 from Arkhas/make_hidden
[9.0] Add the possibility to makeHidden() some attribute of a model.
2 parents 107c67d + 90231b1 commit 7512bc4

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

src/DataTableAbstract.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,13 @@ abstract class DataTableAbstract implements DataTable, Arrayable, Jsonable
4949
* @var array
5050
*/
5151
protected $columnDef = [
52-
'index' => false,
53-
'append' => [],
54-
'edit' => [],
55-
'filter' => [],
56-
'order' => [],
57-
'only' => null,
52+
'index' => false,
53+
'append' => [],
54+
'edit' => [],
55+
'filter' => [],
56+
'order' => [],
57+
'only' => null,
58+
'hidden' => [],
5859
];
5960

6061
/**
@@ -246,6 +247,19 @@ public function escapeColumns($columns = '*')
246247
return $this;
247248
}
248249

250+
/**
251+
* Add a makeHidden() to the row object.
252+
*
253+
* @param array $attributes
254+
* @return $this
255+
*/
256+
public function makeHidden(array $attributes = [])
257+
{
258+
$this->columnDef['hidden'] = array_merge_recursive(array_get($this->columnDef, '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: 2 additions & 1 deletion
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['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, ['hidden' => $this->makeHidden]);
102103
$value = $this->addColumns($data, $row);
103104
$value = $this->editColumns($value, $row);
104105
$value = $this->setupRowVariables($value, $row);

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(array_get($filters, 'hidden', [])) : $row;
158160
$data = $row instanceof Arrayable ? $row->toArray() : (array) $row;
159161

160162
foreach ($data as &$value) {

0 commit comments

Comments
 (0)