Skip to content

Commit a649582

Browse files
committed
Relations has() fixed
- Tests passing
1 parent a1f8bbd commit a649582

File tree

4 files changed

+24
-26
lines changed

4 files changed

+24
-26
lines changed

src/Relations/Traits/QueriesRelationships.php

+13-15
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,8 @@ trait QueriesRelationships
2727
*
2828
* @throws Exception
2929
*/
30-
public function has(
31-
$relation,
32-
$operator = '>=',
33-
$count = 1,
34-
$boolean = 'and',
35-
?Closure $callback = null
36-
): Builder|static {
30+
public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', ?Closure $callback = null): Builder|static
31+
{
3732
if (is_string($relation)) {
3833
if (str_contains($relation, '.')) {
3934
// @phpstan-ignore-next-line
@@ -79,13 +74,8 @@ protected function isAcrossConnections(Relation $relation): bool
7974
*
8075
* @throws Exception
8176
*/
82-
public function addHybridHas(
83-
Relation $relation,
84-
string $operator = '>=',
85-
int $count = 1,
86-
string $boolean = 'and',
87-
?Closure $callback = null
88-
): mixed {
77+
public function addHybridHas(Relation $relation, string $operator = '>=', int $count = 1, string $boolean = 'and', ?Closure $callback = null): mixed
78+
{
8979
$hasQuery = $relation->getQuery();
9080
if ($callback) {
9181
$hasQuery->callScope($callback);
@@ -102,14 +92,22 @@ public function addHybridHas(
10292
$relation instanceof MorphToMany => $relation->getInverse() ?
10393
$this->handleMorphedByMany($hasQuery, $relation) :
10494
$this->handleMorphToMany($hasQuery, $relation),
105-
default => $hasQuery->pluck($this->getHasCompareKey($relation))
95+
default => $this->handleDefaultForeignIdsLookup($hasQuery, $relation)
10696
};
10797

10898
$relatedIds = $this->getConstrainedRelatedIds($relations, $operator, $count);
10999

110100
return $this->whereIn($this->getRelatedConstraintKey($relation), $relatedIds, $boolean, $not);
111101
}
112102

103+
private function handleDefaultForeignIdsLookup($query, $relation)
104+
{
105+
$key = $this->getHasCompareKey($relation);
106+
$query->whereNotNull($key);
107+
108+
return $query->pluck($key);
109+
}
110+
113111
/**
114112
* @param Builder $hasQuery
115113
* @param Relation $relation

tests/Models/Group.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class Group extends Model
1717

1818
protected static $unguarded = true;
1919

20-
public function users(): BelongsToMany
20+
public function groupUsers(): BelongsToMany
2121
{
2222
return $this->belongsToMany(User::class, 'users', 'groups', 'users', 'id', 'id', 'users');
2323
}

tests/Models/User.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function clients()
8989
return $this->belongsToMany(Client::class);
9090
}
9191

92-
public function groups()
92+
public function userGroups()
9393
{
9494
return $this->belongsToMany(Group::class, 'groups', 'users', 'groups', 'id', 'id', 'groups');
9595
}

tests/RelationsTest.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -321,22 +321,22 @@
321321

322322
it('tests belongs to many custom', function () {
323323
$user = User::create(['name' => 'John Doe']);
324-
$group = $user->groups()->create(['name' => 'Admins']);
324+
$group = $user->userGroups()->create(['name' => 'Admins']);
325325

326-
// Refetch
326+
// Re-fetch
327327
$user = User::find($user->id);
328328
$group = Group::find($group->id);
329329

330330
// Check for custom relation attributes
331331
expect($group->getAttributes())->toHaveKey('users')
332332
->and($user->getAttributes())->toHaveKey('groups')
333-
->and($user->groups->pluck('id')->toArray())->toContain($group->id)
334-
->and($group->users->pluck('id')->toArray())->toContain($user->id)
335-
->and($user->groups()->first()->id)->toBe($group->id)
336-
->and($group->users()->first()->id)->toBe($user->id);
333+
->and($user->userGroups->pluck('id')->toArray())->toContain($group->id)
334+
->and($group->groupUsers->pluck('id')->toArray())->toContain($user->id)
335+
->and($user->userGroups()->first()->id)->toBe($group->id)
336+
->and($group->groupUsers()->first()->id)->toBe($user->id);
337337

338338
// Assert they are attached
339-
})->todo();
339+
});
340340

341341
it('tests morph', function () {
342342
$user = User::create(['name' => 'John Doe']);
@@ -854,7 +854,7 @@
854854
$query->where('rating', '<', 5);
855855
})->get();
856856
expect($authors)->toHaveCount(1);
857-
})->todo();
857+
});
858858

859859
it('tests has one has', function () {
860860
$user1 = User::create(['name' => 'John Doe']);
@@ -874,7 +874,7 @@
874874

875875
$users = User::has('role', '!=', 0)->get();
876876
expect($users)->toHaveCount(2);
877-
})->todo();
877+
});
878878

879879
it('tests nested keys', function () {
880880
$client = Client::create([

0 commit comments

Comments
 (0)