Skip to content

Commit 678301f

Browse files
Merge pull request #52 from biiiiiigmonster/fix/has-one-through-laravel11
fixed `HasOneThrough` relationship on laravel-11's change.
2 parents 5d5e868 + e73a0c9 commit 678301f

File tree

5 files changed

+80
-11
lines changed

5 files changed

+80
-11
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ docs
77
phpunit.xml
88
testbench.yaml
99
vendor
10-
node_modules
10+
node_modules
11+
.phpunit.cache

.scrutinizer.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ build:
1717
image: default-bionic
1818
environment:
1919
php:
20-
version: 8.1
20+
version: 8.2
2121
tests:
2222
override:
2323
- command: './vendor/bin/pest'

src/Database/Eloquent/RelationMixin.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
1010
use Illuminate\Database\Eloquent\Relations\HasOne;
1111
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;
12+
use Illuminate\Database\Eloquent\Relations\HasOneThrough;
1213
use Illuminate\Database\Eloquent\Relations\MorphOne;
1314
use Illuminate\Database\Eloquent\Relations\MorphOneOrMany;
1415
use Illuminate\Database\Eloquent\Relations\MorphToMany;
@@ -62,7 +63,7 @@ public function getRelationExistenceInQuery(): Closure
6263

6364
return $relation($query, $parentQuery, $columns);
6465
};
65-
$hasManyThrough = function (Builder $query, Builder $parentQuery) use ($relation): Builder {
66+
$hasOneOrManyThrough = function (Builder $query, Builder $parentQuery) use ($relation): Builder {
6667
$columns = $this->getQualifiedFirstKeyName();
6768
if ($parentQuery->getQuery()->from === $query->getQuery()->from) {
6869
$query->from($query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash());
@@ -132,7 +133,7 @@ public function getRelationExistenceInQuery(): Closure
132133
$this instanceof BelongsTo => $belongsTo($query, $parentQuery),
133134
$this instanceof BelongsToMany => $belongsToMany($query, $parentQuery),
134135
$this instanceof HasOneOrMany => $hasOneOrMany($query, $parentQuery),
135-
$this instanceof HasManyThrough => $hasManyThrough($query, $parentQuery),
136+
$this instanceof HasOneThrough, $this instanceof HasManyThrough => $hasOneOrManyThrough($query, $parentQuery),
136137
default => throw new LogicException(
137138
sprintf('%s must be a relationship instance.', $this::class)
138139
)
@@ -145,7 +146,7 @@ public function getRelationWhereInKey(): Closure
145146
return fn (): string => match (true) {
146147
$this instanceof BelongsTo => $this->getQualifiedForeignKeyName(),
147148
$this instanceof HasOneOrMany, $this instanceof BelongsToMany => $this->getQualifiedParentKeyName(),
148-
$this instanceof HasManyThrough => $this->getQualifiedLocalKeyName(),
149+
$this instanceof HasOneThrough, $this instanceof HasManyThrough => $this->getQualifiedLocalKeyName(),
149150
default => throw new LogicException(
150151
sprintf('%s must be a relationship instance.', $this::class)
151152
)

tests/Features/HasInTest.php

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,81 @@
11
<?php
22

3+
use BiiiiiigMonster\Hasin\Tests\Models\Country;
4+
use BiiiiiigMonster\Hasin\Tests\Models\Image;
5+
use BiiiiiigMonster\Hasin\Tests\Models\Supplier;
36
use BiiiiiigMonster\Hasin\Tests\Models\User;
7+
use BiiiiiigMonster\Hasin\Tests\Models\Video;
48

5-
test('hasIn same as has', function () {
9+
test('HasMany: hasIn same as has', function () {
610
$has = User::has('posts')->orderBy('id')->pluck('id');
711
$hasIn = User::hasIn('posts')->orderBy('id')->pluck('id');
812

913
expect($has)->toEqual($hasIn);
1014
});
1115

16+
test('HasOne: hasIn same as has', function () {
17+
$has = User::has('phone')->orderBy('id')->pluck('id');
18+
$hasIn = User::hasIn('phone')->orderBy('id')->pluck('id');
19+
20+
expect($has)->toEqual($hasIn);
21+
});
22+
23+
test('BelongsTo: hasIn same as has', function () {
24+
$has = User::has('country')->orderBy('id')->pluck('id');
25+
$hasIn = User::hasIn('country')->orderBy('id')->pluck('id');
26+
27+
expect($has)->toEqual($hasIn);
28+
});
29+
30+
test('BelongsToMany: hasIn same as has', function () {
31+
$has = User::has('roles')->orderBy('id')->pluck('id');
32+
$hasIn = User::hasIn('roles')->orderBy('id')->pluck('id');
33+
34+
expect($has)->toEqual($hasIn);
35+
});
36+
37+
test('HasOneThrough: hasIn same as has', function () {
38+
$has = Supplier::has('userHistory')->orderBy('id')->pluck('id');
39+
$hasIn = Supplier::hasIn('userHistory')->orderBy('id')->pluck('id');
40+
41+
expect($has)->toEqual($hasIn);
42+
});
43+
44+
test('HasManyThrough: hasIn same as has', function () {
45+
$has = Country::has('posts')->orderBy('id')->pluck('id');
46+
$hasIn = Country::hasIn('posts')->orderBy('id')->pluck('id');
47+
48+
expect($has)->toEqual($hasIn);
49+
});
50+
51+
test('MorphOne: hasIn same as has', function () {
52+
$has = User::has('image')->orderBy('id')->pluck('id');
53+
$hasIn = User::hasIn('image')->orderBy('id')->pluck('id');
54+
55+
expect($has)->toEqual($hasIn);
56+
});
57+
58+
test('MorphTo: hasIn same as has', function () {
59+
$has = Image::has('imageable')->orderBy('id')->pluck('id');
60+
$hasIn = Image::hasIn('imageable')->orderBy('id')->pluck('id');
61+
62+
expect($has)->toEqual($hasIn);
63+
});
64+
65+
test('MorphMany: hasIn same as has', function () {
66+
$has = Video::has('comments')->orderBy('id')->pluck('id');
67+
$hasIn = Video::hasIn('comments')->orderBy('id')->pluck('id');
68+
69+
expect($has)->toEqual($hasIn);
70+
});
71+
72+
test('MorphToMany: hasIn same as has', function () {
73+
$has = Video::has('tags')->orderBy('id')->pluck('id');
74+
$hasIn = Video::hasIn('tags')->orderBy('id')->pluck('id');
75+
76+
expect($has)->toEqual($hasIn);
77+
});
78+
1279
test('hasIn(gte 2) same as has(gte 2)', function () {
1380
$has = User::has('posts', '>=', 2)->orderBy('id')->pluck('id');
1481
$hasIn = User::hasIn('posts', '>=', 2)->orderBy('id')->pluck('id');

tests/Features/WithWhereHasInTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
$query->where('votes', '>', 20);
1212
})->orderBy('id')->get();
1313

14-
expect($whereHas->pluck('id'))->toEqual($whereHasIn->pluck('id'));
15-
expect($whereHas->pluck('posts.id'))->toEqual($whereHasIn->pluck('posts.id'));
14+
expect($whereHas->pluck('id'))->toEqual($whereHasIn->pluck('id'))
15+
->and($whereHas->pluck('posts.id'))->toEqual($whereHasIn->pluck('posts.id'));
1616
});
1717

1818
test('nested withWhereHasIn same as nested withWhereHas', function () {
@@ -23,7 +23,7 @@
2323
$query->where('status', '>', 2);
2424
})->orderBy('id')->get();
2525

26-
expect($whereHas->pluck('id'))->toEqual($whereHasIn->pluck('id'));
27-
expect($whereHas->pluck('posts.id'))->toEqual($whereHasIn->pluck('posts.id'));
28-
expect($whereHas->pluck('posts.comments.id'))->toEqual($whereHasIn->pluck('posts.comments.id'));
26+
expect($whereHas->pluck('id'))->toEqual($whereHasIn->pluck('id'))
27+
->and($whereHas->pluck('posts.id'))->toEqual($whereHasIn->pluck('posts.id'))
28+
->and($whereHas->pluck('posts.comments.id'))->toEqual($whereHasIn->pluck('posts.comments.id'));
2929
});

0 commit comments

Comments
 (0)