Skip to content

Commit cadaa7c

Browse files
authored
Merge pull request yajra#2103 from selecod/8.0
[8.0] Removal of redundant SoftDelete check.
2 parents 0b6f9c0 + 9b66f4e commit cadaa7c

File tree

6 files changed

+107
-19
lines changed

6 files changed

+107
-19
lines changed

src/EloquentDataTable.php

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ protected function isNotEagerLoaded($relation)
138138
protected function joinEagerLoadedColumn($relation, $relationColumn)
139139
{
140140
$table = '';
141-
$deletedAt = false;
142141
$lastQuery = $this->query;
143142
foreach (explode('.', $relation) as $eachRelation) {
144143
$model = $lastQuery->getRelation($eachRelation);
@@ -164,45 +163,33 @@ protected function joinEagerLoadedColumn($relation, $relationColumn)
164163
$table = $model->getRelated()->getTable();
165164
$foreign = $model->getQualifiedForeignKeyName();
166165
$other = $model->getQualifiedParentKeyName();
167-
$deletedAt = $this->checkSoftDeletesOnModel($model->getRelated());
168166
break;
169167

170168
case $model instanceof BelongsTo:
171169
$table = $model->getRelated()->getTable();
172170
$foreign = $model->getQualifiedForeignKey();
173171
$other = $model->getQualifiedOwnerKeyName();
174-
$deletedAt = $this->checkSoftDeletesOnModel($model->getRelated());
175172
break;
176173

177174
default:
178175
throw new Exception('Relation ' . get_class($model) . ' is not yet supported.');
179176
}
180-
$this->performJoin($table, $foreign, $other, $deletedAt);
177+
$this->performJoin($table, $foreign, $other);
181178
$lastQuery = $model->getQuery();
182179
}
183180

184181
return $table . '.' . $relationColumn;
185182
}
186183

187-
protected function checkSoftDeletesOnModel($model)
188-
{
189-
if (in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($model))) {
190-
return $model->getQualifiedDeletedAtColumn();
191-
}
192-
193-
return false;
194-
}
195-
196184
/**
197185
* Perform join query.
198186
*
199187
* @param string $table
200188
* @param string $foreign
201189
* @param string $other
202-
* @param string $deletedAt
203190
* @param string $type
204191
*/
205-
protected function performJoin($table, $foreign, $other, $deletedAt = false, $type = 'left')
192+
protected function performJoin($table, $foreign, $other, $type = 'left')
206193
{
207194
$joins = [];
208195
foreach ((array) $this->getBaseQueryBuilder()->joins as $key => $join) {
@@ -212,9 +199,5 @@ protected function performJoin($table, $foreign, $other, $deletedAt = false, $ty
212199
if (! in_array($table, $joins)) {
213200
$this->getBaseQueryBuilder()->join($table, $foreign, '=', $other, $type);
214201
}
215-
216-
if ($deletedAt !== false) {
217-
$this->getBaseQueryBuilder()->whereNull($deletedAt);
218-
}
219202
}
220203
}

tests/Integration/HasManyRelationTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Yajra\DataTables\DataTables;
66
use Yajra\DataTables\Tests\TestCase;
7+
use Yajra\DataTables\Tests\Models\Post;
78
use Yajra\DataTables\Tests\Models\User;
89
use Illuminate\Foundation\Testing\DatabaseTransactions;
910

@@ -25,6 +26,37 @@ public function it_returns_all_records_with_the_relation_when_called_without_par
2526
$this->assertCount(20, $response->json()['data']);
2627
}
2728

29+
/** @test */
30+
public function it_returns_all_records_with_deleted_relations_when_called_with_withtrashed_parameter()
31+
{
32+
Post::find(1)->delete();
33+
34+
$response = $this->call('GET', '/relations/hasManyWithTrashed');
35+
$response->assertJson([
36+
'draw' => 0,
37+
'recordsTotal' => 20,
38+
'recordsFiltered' => 20,
39+
]);
40+
41+
$this->assertArrayHasKey('posts', $response->json()['data'][0]);
42+
$this->assertCount(3, $response->json()['data'][0]['posts']);
43+
}
44+
45+
/** @test */
46+
public function it_returns_all_records_with_only_deleted_relations_when_called_with_onlytrashed_parameter()
47+
{
48+
Post::find(1)->delete();
49+
$response = $this->call('GET', '/relations/hasManyOnlyTrashed');
50+
$response->assertJson([
51+
'draw' => 0,
52+
'recordsTotal' => 20,
53+
'recordsFiltered' => 20,
54+
]);
55+
56+
$this->assertArrayHasKey('posts', $response->json()['data'][0]);
57+
$this->assertCount(1, $response->json()['data'][0]['posts']);
58+
}
59+
2860
/** @test */
2961
public function it_can_perform_global_search_on_the_relation()
3062
{
@@ -60,5 +92,17 @@ protected function setUp()
6092
$this->app['router']->get('/relations/hasMany', function (DataTables $datatables) {
6193
return $datatables->eloquent(User::with('posts')->select('users.*'))->toJson();
6294
});
95+
96+
$this->app['router']->get('/relations/hasManyWithTrashed', function (DataTables $datatables) {
97+
return $datatables->eloquent(User::with(['posts' => function ($query) {
98+
$query->withTrashed();
99+
}])->select('users.*'))->toJson();
100+
});
101+
102+
$this->app['router']->get('/relations/hasManyOnlyTrashed', function (DataTables $datatables) {
103+
return $datatables->eloquent(User::with(['posts' => function ($query) {
104+
$query->onlyTrashed();
105+
}])->select('users.*'))->toJson();
106+
});
63107
}
64108
}

tests/Integration/HasOneRelationTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Yajra\DataTables\DataTables;
66
use Yajra\DataTables\Tests\TestCase;
77
use Yajra\DataTables\Tests\Models\User;
8+
use Yajra\DataTables\Tests\Models\Heart;
89
use Illuminate\Foundation\Testing\DatabaseTransactions;
910

1011
class HasOneRelationTest extends TestCase
@@ -25,6 +26,42 @@ public function it_returns_all_records_with_the_relation_when_called_without_par
2526
$this->assertCount(20, $response->json()['data']);
2627
}
2728

29+
/** @test */
30+
public function it_returns_all_records_with_the_deleted_relation_when_called_with_withtrashed_parameter()
31+
{
32+
Heart::find(1)->delete();
33+
34+
$response = $this->call('GET', '/relations/hasOneWithTrashed');
35+
$response->assertJson([
36+
'draw' => 0,
37+
'recordsTotal' => 20,
38+
'recordsFiltered' => 20,
39+
]);
40+
41+
$this->assertArrayHasKey('heart', $response->json()['data'][0]);
42+
$this->assertArrayHasKey('heart', $response->json()['data'][1]);
43+
$this->assertNotEmpty($response->json()['data'][0]['heart']);
44+
$this->assertNotEmpty($response->json()['data'][1]['heart']);
45+
}
46+
47+
/** @test */
48+
public function it_returns_all_records_with_the_only_deleted_relation_when_called_with_onlytrashed_parameter()
49+
{
50+
Heart::find(1)->delete();
51+
52+
$response = $this->call('GET', '/relations/hasOneOnlyTrashed');
53+
$response->assertJson([
54+
'draw' => 0,
55+
'recordsTotal' => 20,
56+
'recordsFiltered' => 20,
57+
]);
58+
59+
$this->assertArrayHasKey('heart', $response->json()['data'][0]);
60+
$this->assertArrayHasKey('heart', $response->json()['data'][1]);
61+
$this->assertNotEmpty($response->json()['data'][0]['heart']);
62+
$this->assertEmpty($response->json()['data'][1]['heart']);
63+
}
64+
2865
/** @test */
2966
public function it_can_perform_global_search_on_the_relation()
3067
{
@@ -86,5 +123,17 @@ protected function setUp()
86123
$this->app['router']->get('/relations/hasOne', function (DataTables $datatables) {
87124
return $datatables->eloquent(User::with('heart')->select('users.*'))->toJson();
88125
});
126+
127+
$this->app['router']->get('/relations/hasOneWithTrashed', function (DataTables $datatables) {
128+
return $datatables->eloquent(User::with(['heart' => function ($query) {
129+
$query->withTrashed();
130+
}])->select('users.*'))->toJson();
131+
});
132+
133+
$this->app['router']->get('/relations/hasOneOnlyTrashed', function (DataTables $datatables) {
134+
return $datatables->eloquent(User::with(['heart' => function ($query) {
135+
$query->onlyTrashed();
136+
}])->select('users.*'))->toJson();
137+
});
89138
}
90139
}

tests/Models/Heart.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
namespace Yajra\DataTables\Tests\Models;
44

55
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\SoftDeletes;
67

78
class Heart extends Model
89
{
10+
use SoftDeletes;
11+
912
protected $guarded = [];
1013

14+
protected $dates = ['deleted_at'];
15+
1116
public function user()
1217
{
1318
return $this->hasOne(User::class);

tests/Models/Post.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
namespace Yajra\DataTables\Tests\Models;
44

55
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\SoftDeletes;
67

78
class Post extends Model
89
{
10+
use SoftDeletes;
11+
912
protected $guarded = [];
1013

14+
protected $dates = ['deleted_at'];
15+
1116
public function user()
1217
{
1318
return $this->belongsTo(User::class);

tests/TestCase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ protected function migrateDatabase()
3636
$table->string('title');
3737
$table->unsignedInteger('user_id');
3838
$table->timestamps();
39+
$table->softDeletes();
3940
});
4041
}
4142
if (! $schemaBuilder->hasTable('hearts')) {
@@ -44,6 +45,7 @@ protected function migrateDatabase()
4445
$table->unsignedInteger('user_id');
4546
$table->string('size');
4647
$table->timestamps();
48+
$table->softDeletes();
4749
});
4850
}
4951
if (! $schemaBuilder->hasTable('roles')) {

0 commit comments

Comments
 (0)