Skip to content

Commit 6e26310

Browse files
authored
Merge pull request #3009 from Arkhas/ignore_getters
feat: add ability to disable eloquent getter mutator
2 parents 17b247d + 3a05a65 commit 6e26310

File tree

6 files changed

+122
-9
lines changed

6 files changed

+122
-9
lines changed

src/DataTableAbstract.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,14 @@ abstract class DataTableAbstract implements DataTable
5252
* @var array
5353
*/
5454
protected array $columnDef = [
55-
'index' => false,
56-
'append' => [],
57-
'edit' => [],
58-
'filter' => [],
59-
'order' => [],
60-
'only' => null,
61-
'hidden' => [],
55+
'index' => false,
56+
'ignore_getters' => false,
57+
'append' => [],
58+
'edit' => [],
59+
'filter' => [],
60+
'order' => [],
61+
'only' => null,
62+
'hidden' => [],
6263
'visible' => [],
6364
];
6465

@@ -224,6 +225,19 @@ public function addIndexColumn(): static
224225
return $this;
225226
}
226227

228+
/**
229+
* Prevent the getters Mutators to be applied when converting a collection
230+
* of the Models into the final JSON.
231+
*
232+
* @return $this
233+
*/
234+
public function ignoreGetters(): static
235+
{
236+
$this->columnDef['ignore_getters'] = true;
237+
238+
return $this;
239+
}
240+
227241
/**
228242
* Edit column's content.
229243
*

src/Processors/DataProcessor.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ class DataProcessor
7878
*/
7979
protected bool $includeIndex = false;
8080

81+
/**
82+
* @var bool
83+
*/
84+
protected bool $ignoreGetters = false;
85+
8186
/**
8287
* @param iterable $results
8388
* @param array $columnDef
@@ -96,6 +101,7 @@ public function __construct($results, array $columnDef, array $templates, int $s
96101
$this->rawColumns = $columnDef['raw'] ?? [];
97102
$this->makeHidden = $columnDef['hidden'] ?? [];
98103
$this->makeVisible = $columnDef['visible'] ?? [];
104+
$this->ignoreGetters = $columnDef['ignore_getters'] ?? false;
99105
$this->templates = $templates;
100106
$this->start = $start;
101107
}
@@ -112,7 +118,7 @@ public function process($object = false): array
112118
$indexColumn = config('datatables.index_column', 'DT_RowIndex');
113119

114120
foreach ($this->results as $row) {
115-
$data = Helper::convertToArray($row, ['hidden' => $this->makeHidden, 'visible' => $this->makeVisible]);
121+
$data = Helper::convertToArray($row, ['hidden' => $this->makeHidden, 'visible' => $this->makeVisible, 'ignore_getters' => $this->ignoreGetters]);
116122
$value = $this->addColumns($data, $row);
117123
$value = $this->editColumns($value, $row);
118124
$value = $this->setupRowVariables($value, $row);

src/Utilities/Helper.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,12 +196,29 @@ public static function getOrMethod($method)
196196
*/
197197
public static function convertToArray($row, $filters = [])
198198
{
199+
if (Arr::get($filters, 'ignore_getters') && is_object($row) && method_exists($row, 'getAttributes')) {
200+
$data = $row->getAttributes();
201+
if (method_exists($row, 'getRelations')) {
202+
foreach ($row->getRelations() as $relationName => $relation) {
203+
if (is_iterable($relation)) {
204+
foreach ($relation as $relationItem) {
205+
$data[$relationName][] = self::convertToArray($relationItem, ['ignore_getters' => true]);
206+
}
207+
} else {
208+
$data[$relationName] = self::convertToArray($relation, ['ignore_getters' => true]);
209+
}
210+
}
211+
}
212+
213+
return $data;
214+
}
215+
199216
$row = is_object($row) && method_exists($row, 'makeHidden') ? $row->makeHidden(Arr::get($filters, 'hidden',
200217
[])) : $row;
201218
$row = is_object($row) && method_exists($row, 'makeVisible') ? $row->makeVisible(Arr::get($filters, 'visible',
202219
[])) : $row;
203-
$data = $row instanceof Arrayable ? $row->toArray() : (array) $row;
204220

221+
$data = $row instanceof Arrayable ? $row->toArray() : (array) $row;
205222
foreach ($data as &$value) {
206223
if (is_object($value) || is_array($value)) {
207224
$value = self::convertToArray($value);
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Yajra\DataTables\Tests\Integration;
4+
5+
use Illuminate\Foundation\Testing\DatabaseTransactions;
6+
use Yajra\DataTables\DataTables;
7+
use Yajra\DataTables\Tests\Models\User;
8+
use Yajra\DataTables\Tests\TestCase;
9+
10+
class IgnoreGettersTest extends TestCase
11+
{
12+
use DatabaseTransactions;
13+
14+
/** @test */
15+
public function it_return_the_default_value_when_attribute_is_null()
16+
{
17+
$user = User::create([
18+
'name' => 'foo',
19+
'email' => '[email protected]',
20+
'color' => null
21+
]);
22+
23+
$this->assertEquals('#000000', $user->color);
24+
$this->assertEquals('#000000', $user->refresh()->toArray()['color']);
25+
}
26+
27+
/** @test */
28+
public function it_return_the_getter_value_without_ignore_getters()
29+
{
30+
$this->app['router']->get('/ignore-getters', function (DataTables $datatables) {
31+
return $datatables->eloquent(User::with('posts.user')->select('users.*'))->toJson();
32+
});
33+
34+
$response = $this->call('GET', '/ignore-getters');
35+
$response->assertJson([
36+
'draw' => 0,
37+
'recordsTotal' => 20,
38+
'recordsFiltered' => 20,
39+
]);
40+
41+
$this->assertNotNull($response->json()['data'][0]['posts']);
42+
// Assert the getter color is call on primary Model
43+
$this->assertNotNull($response->json()['data'][0]['color']);
44+
// Assert the getter color is call on relationships
45+
$this->assertNotNull($response->json()['data'][0]['posts'][0]['user']['color']);
46+
$this->assertCount(20, $response->json()['data']);
47+
}
48+
49+
/** @test */
50+
public function it_ignore_the_getter_value_with_ignore_getters()
51+
{
52+
$this->app['router']->get('/ignore-getters', function (DataTables $datatables) {
53+
return $datatables->eloquent(User::with('posts.user')->select('users.*'))->ignoreGetters()->toJson();
54+
});
55+
56+
$response = $this->call('GET', '/ignore-getters');
57+
$response->assertJson([
58+
'draw' => 0,
59+
'recordsTotal' => 20,
60+
'recordsFiltered' => 20,
61+
]);
62+
63+
$this->assertNotNull($response->json()['data'][0]['posts']);
64+
// Assert the getter color is not call on primary Model
65+
$this->assertNull($response->json()['data'][0]['color']);
66+
// Assert the getter color is not call on relationships
67+
$this->assertNull($response->json()['data'][0]['posts'][0]['user']['color']);
68+
$this->assertCount(20, $response->json()['data']);
69+
}
70+
}

tests/Models/User.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,9 @@ public function user()
2727
{
2828
return $this->morphTo();
2929
}
30+
31+
public function getColorAttribute()
32+
{
33+
return $this->color ?? '#000000';
34+
}
3035
}

tests/TestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ protected function migrateDatabase()
2929
$table->increments('id');
3030
$table->string('name');
3131
$table->string('email');
32+
$table->string('color')->nullable();
3233
$table->string('user_type')->nullable();
3334
$table->unsignedInteger('user_id')->nullable();
3435
$table->timestamps();

0 commit comments

Comments
 (0)