Skip to content

Commit 70bf326

Browse files
authored
Merge pull request #92 from yajra/patch
fix: editor stub with generics
2 parents e81b04b + 1ba068f commit 70bf326

File tree

8 files changed

+74
-11
lines changed

8 files changed

+74
-11
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
},
2626
"require-dev": {
2727
"larastan/larastan": "^3.1",
28-
"laravel/pint": "^1.21",
29-
"orchestra/testbench": "^10.0",
30-
"rector/rector": "^2.0.9"
28+
"laravel/pint": "^1.21.2",
29+
"orchestra/testbench": "^10.1",
30+
"rector/rector": "^2.0.10"
3131
},
3232
"autoload": {
3333
"psr-4": {

phpstan.neon.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ parameters:
1515
- identifier: binaryOp.invalid
1616
- identifier: offsetAccess.nonOffsetAccessible
1717
- identifier: return.type
18-
- identifier: method.nonObject
1918
- identifier: assign.propertyType
2019

2120
excludePaths:

src/DataTablesEditor.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,11 @@ protected function getBuilder(): Builder
223223
*/
224224
protected function resolveModel(): Model
225225
{
226-
if (! $this->model instanceof Model) {
226+
if (is_null($this->model)) {
227+
throw new DataTablesEditorException('Model not set.');
228+
}
229+
230+
if (is_string($this->model)) {
227231
$this->model = new $this->model;
228232
}
229233

@@ -233,9 +237,7 @@ protected function resolveModel(): Model
233237
}
234238

235239
/**
236-
* Set model unguard state.
237-
*
238-
* @return $this
240+
* Set model unguarded state.
239241
*/
240242
public function unguard(bool $state = true): static
241243
{

src/Generators/stubs/editor.stub

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ use Illuminate\Database\Eloquent\Model;
99
use Illuminate\Validation\Rule;
1010
use Yajra\DataTables\DataTablesEditor;
1111

12+
/**
13+
* @extends DataTablesEditor<ModelName>
14+
*/
1215
class DummyClass extends DataTablesEditor
1316
{
1417
protected $model = ModelName::class;

tests/Feature/DataTablesEditorCreateTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,37 @@ public function it_can_process_create_request()
2424

2525
$data = $response->json()['data'][0];
2626
$this->assertArrayHasKey('id', $data);
27+
$this->assertEquals(1, $data['DT_RowId']);
2728
$this->assertEquals(1, $data['id']);
2829
$this->assertEquals('Taylor', $data['name']);
2930
$this->assertEquals('taylor@laravel.com', $data['email']);
3031
}
3132

33+
#[Test]
34+
public function it_does_not_process_not_fillable_columns()
35+
{
36+
$response = $this->postJson('users', [
37+
'action' => 'create',
38+
'data' => [
39+
0 => [
40+
'name' => 'Taylor',
41+
'email' => 'taylor@laravel.com',
42+
'role' => 'admin',
43+
],
44+
],
45+
]);
46+
47+
$this->assertDatabaseHas('users', ['id' => 1]);
48+
49+
$data = $response->json()['data'][0];
50+
$this->assertArrayHasKey('id', $data);
51+
$this->assertEquals(1, $data['DT_RowId']);
52+
$this->assertEquals(1, $data['id']);
53+
$this->assertEquals('Taylor', $data['name']);
54+
$this->assertEquals('taylor@laravel.com', $data['email']);
55+
$this->assertNotContains('role', $data);
56+
}
57+
3258
#[Test]
3359
public function it_allows_created_callback_and_returns_the_modified_model()
3460
{
@@ -77,4 +103,29 @@ public function it_can_validate_invalid_inputs()
77103
$this->assertEquals('name', $errors[1]['name']);
78104
$this->assertEquals('The name field is required.', $errors[1]['status']);
79105
}
106+
107+
#[Test]
108+
public function it_can_set_the_model_to_unguarded_state(): void
109+
{
110+
$response = $this->postJson('users-unguarded', [
111+
'action' => 'create',
112+
'data' => [
113+
0 => [
114+
'name' => 'Taylor',
115+
'email' => 'taylor@laravel.com',
116+
'role' => 'admin',
117+
],
118+
],
119+
]);
120+
121+
$this->assertDatabaseHas('users', ['id' => 1]);
122+
123+
$data = $response->json()['data'][0];
124+
$this->assertArrayHasKey('id', $data);
125+
$this->assertEquals(1, $data['DT_RowId']);
126+
$this->assertEquals(1, $data['id']);
127+
$this->assertEquals('Taylor', $data['name']);
128+
$this->assertEquals('taylor@laravel.com', $data['email']);
129+
$this->assertEquals('admin', $data['role']);
130+
}
80131
}

tests/Models/Post.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
namespace Yajra\DataTables\Tests\Models;
44

55
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
67

78
class Post extends Model
89
{
910
protected $guarded = [];
1011

11-
public function user()
12+
public function user(): BelongsToMany
1213
{
1314
return $this->belongsToMany(User::class);
1415
}

tests/Models/User.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
namespace Yajra\DataTables\Tests\Models;
44

55
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\Relations\HasMany;
67

78
class User extends Model
89
{
9-
protected $guarded = [];
10+
protected $fillable = [
11+
'name',
12+
'email',
13+
'password',
14+
];
1015

11-
public function posts()
16+
public function posts(): HasMany
1217
{
1318
return $this->hasMany(Post::class);
1419
}

tests/TestCase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ protected function migrateDatabase(): void
3131
$table->string('name');
3232
$table->string('email');
3333
$table->string('password')->nullable();
34+
$table->string('role')->nullable();
3435
$table->timestamps();
3536
});
3637
}
3738

3839
protected function defineRoutes($router): void
3940
{
4041
$router->post('users', fn (UsersDataTableEditor $editor, Request $request) => $editor->process($request));
42+
$router->post('users-unguarded', fn (UsersDataTableEditor $editor, Request $request) => $editor->unguard()->process($request));
4143

4244
$router->post(
4345
'usersWithEvents',

0 commit comments

Comments
 (0)