Skip to content

Commit 3f57b39

Browse files
authored
Using model scopes instead of checking the soft delete scope trait directly (#183)
1 parent 6de51d9 commit 3f57b39

6 files changed

+60
-12
lines changed

src/Mixins/QueryRelationshipExistence.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ public function getGroupBy()
1313
};
1414
}
1515

16+
public function getScopes()
17+
{
18+
return function () {
19+
return $this->scopes;
20+
};
21+
}
22+
1623
public function getSelect()
1724
{
1825
return function () {

src/Mixins/RelationshipsExtraMethods.php

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Kirschbaum\PowerJoins\Mixins;
44

5+
use Illuminate\Database\Eloquent\Model;
56
use Stringable;
67
use Illuminate\Support\Str;
78
use Kirschbaum\PowerJoins\StaticCache;
@@ -16,6 +17,7 @@
1617
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
1718
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
1819
use Illuminate\Database\Eloquent\Relations\MorphOneOrMany;
20+
use Illuminate\Database\Eloquent\SoftDeletingScope;
1921

2022
/**
2123
* @method \Illuminate\Database\Eloquent\Model getModel()
@@ -86,7 +88,7 @@ protected function performJoinForEloquentPowerJoinsForBelongsTo()
8688
"{$joinedTable}.{$this->ownerKey}"
8789
);
8890

89-
if ($disableExtraConditions === false && $this->usesSoftDeletes($this->query->getModel())) {
91+
if ($disableExtraConditions === false && $this->usesSoftDeletes($this->query->getScopes())) {
9092
$join->whereNull("{$joinedTable}.{$this->query->getModel()->getDeletedAtColumn()}");
9193
}
9294

@@ -139,7 +141,7 @@ protected function performJoinForEloquentPowerJoinsForBelongsToMany()
139141
"{$joinedTable}.{$this->getRelatedPivotKeyName()}"
140142
);
141143

142-
if ($disableExtraConditions === false && $this->usesSoftDeletes($this->query->getModel())) {
144+
if ($disableExtraConditions === false && $this->usesSoftDeletes($this->query->getScopes())) {
143145
$join->whereNull($this->query->getModel()->getQualifiedDeletedAtColumn());
144146
}
145147

@@ -200,7 +202,7 @@ protected function performJoinForEloquentPowerJoinsForMorphToMany()
200202
"{$joinedTable}.{$this->getRelatedPivotKeyName()}"
201203
);
202204

203-
if ($disableExtraConditions === false && $this->usesSoftDeletes($this->query->getModel())) {
205+
if ($disableExtraConditions === false && $this->usesSoftDeletes($this->query->getScopes())) {
204206
$join->whereNull($this->query->getModel()->getQualifiedDeletedAtColumn());
205207
}
206208

@@ -226,7 +228,7 @@ protected function performJoinForEloquentPowerJoinsForMorph()
226228
"{$this->parent->getTable()}.{$this->localKey}"
227229
)->where("{$this->getModel()->getTable()}.{$this->getMorphType()}", '=', $this->getMorphClass());
228230

229-
if ($disableExtraConditions === false && $this->usesSoftDeletes($this->query->getModel())) {
231+
if ($disableExtraConditions === false && $this->usesSoftDeletes($this->query->getScopes())) {
230232
$join->whereNull($this->query->getModel()->getQualifiedDeletedAtColumn());
231233
}
232234

@@ -249,6 +251,7 @@ protected function performJoinForEloquentPowerJoinsForMorph()
249251
protected function performJoinForEloquentPowerJoinsForMorphTo()
250252
{
251253
return function ($builder, $joinType, $callback = null, $alias = null, bool $disableExtraConditions = false, string $morphable = null) {
254+
/** @var Model */
252255
$modelInstance = new $morphable;
253256

254257
$builder->{$joinType}($modelInstance->getTable(), function ($join) use ($modelInstance, $callback, $disableExtraConditions) {
@@ -258,7 +261,7 @@ protected function performJoinForEloquentPowerJoinsForMorphTo()
258261
"{$modelInstance->getTable()}.{$modelInstance->getKeyName()}"
259262
)->where("{$this->getModel()->getTable()}.{$this->getMorphType()}", '=', $modelInstance->getMorphClass());
260263

261-
if ($disableExtraConditions === false && $this->usesSoftDeletes($modelInstance)) {
264+
if ($disableExtraConditions === false && $this->usesSoftDeletes($modelInstance->getScopes())) {
262265
$join->whereNull($modelInstance->getQualifiedDeletedAtColumn());
263266
}
264267

@@ -304,7 +307,7 @@ protected function performJoinForEloquentPowerJoinsForHasMany()
304307
"{$parentTable}.{$this->localKey}"
305308
);
306309

307-
if ($disableExtraConditions === false && $this->usesSoftDeletes($this->query->getModel())) {
310+
if ($disableExtraConditions === false && $this->usesSoftDeletes($this->query->getScopes())) {
308311
$join->whereNull(
309312
"{$joinedTable}.{$this->query->getModel()->getDeletedAtColumn()}"
310313
);
@@ -370,7 +373,7 @@ protected function performJoinForEloquentPowerJoinsForHasManyThrough()
370373
"{$throughTable}.{$this->secondLocalKey}"
371374
);
372375

373-
if ($this->usesSoftDeletes($this->getModel())) {
376+
if ($this->usesSoftDeletes($this->getScopes())) {
374377
$join->whereNull("{$farTable}.{$this->getModel()->getDeletedAtColumn()}");
375378
}
376379

@@ -408,8 +411,15 @@ public function performHavingForEloquentPowerJoins()
408411
*/
409412
public function usesSoftDeletes()
410413
{
414+
/**
415+
* @param \Illuminate\Database\Eloquent\Model|array $model
416+
*/
411417
return function ($model) {
412-
return in_array(SoftDeletes::class, class_uses_recursive($model));
418+
if ($model instanceof Model) {
419+
return in_array(SoftDeletes::class, class_uses_recursive($model));
420+
}
421+
422+
return array_key_exists(SoftDeletingScope::class, $model);
413423
};
414424
}
415425

src/PowerJoinClause.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,18 +219,27 @@ public function withTrashed(): self
219219
*/
220220
public function onlyTrashed(): self
221221
{
222-
if (! $this->getModel() || ! in_array(SoftDeletes::class, class_uses_recursive($this->getModel()))) {
222+
if (! $this->getModel()
223+
|| ! in_array(SoftDeletes::class, class_uses_recursive($this->getModel()))
224+
) {
223225
return $this;
224226
}
225227

226-
$this->wheres = array_map(function ($where) {
228+
$hasCondition = null;
229+
230+
$this->wheres = array_map(function ($where) use (&$hasCondition) {
227231
if ($where['type'] === 'Null' && Str::contains($where['column'], $this->getModel()->getDeletedAtColumn())) {
228232
$where['type'] = 'NotNull';
233+
$hasCondition = true;
229234
}
230235

231236
return $where;
232237
}, $this->wheres);
233238

239+
if (! $hasCondition) {
240+
$this->whereNotNull($this->getModel()->getQualifiedDeletedAtColumn());
241+
}
242+
234243
return $this;
235244
}
236245

tests/JoinRelationshipExtraConditionsTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ public function test_join_belongs_to_with_additional_conditions()
2424
$query = Post::query()->joinRelationship('userWithTrashed')->toSql();
2525
$posts = Post::query()->joinRelationship('userWithTrashed')->get();
2626

27-
$this->assertCount(1, $posts);
2827

2928
$this->assertStringContainsString(
30-
'inner join "users" on "posts"."user_id" = "users"."id" and "users"."deleted_at" is null',
29+
'inner join "users" on "posts"."user_id" = "users"."id"',
3130
$query
3231
);
32+
33+
$this->assertCount(2, $posts);
3334
}
3435

3536
/** @test */

tests/Models/User.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public function posts(): HasMany
3939
return $this->hasMany(Post::class);
4040
}
4141

42+
public function postsWithTrashed(): HasMany
43+
{
44+
return $this->hasMany(Post::class)->withTrashed();
45+
}
46+
4247
public function publishedPosts(): HasMany
4348
{
4449
return $this->hasMany(Post::class)->where(function ($query) {

tests/SoftDeletesTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,20 @@ public function it_can_disable_soft_deletes_when_using_an_alias()
8989
$query
9090
);
9191
}
92+
93+
public function test_it_respects_with_trashed()
94+
{
95+
User::query()->joinRelationship('postsWithTrashed')->get();
96+
$sql = User::query()->joinRelationship('postsWithTrashed')->toSql();
97+
98+
$this->assertStringContainsString(
99+
'inner join "posts" on "posts"."user_id" = "users"."id"',
100+
$sql
101+
);
102+
103+
$this->assertStringNotContainsString(
104+
'"posts"."deleted_at" is null',
105+
$sql
106+
);
107+
}
92108
}

0 commit comments

Comments
 (0)