From 2264cbbe43721b688a48083031e5a86d944b48f8 Mon Sep 17 00:00:00 2001 From: Ashley Shenton Date: Wed, 7 May 2025 09:27:47 +0100 Subject: [PATCH 1/3] test: update whereAttachedTo builder tests to showcase the issue --- tests/Database/DatabaseEloquentBuilderTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Database/DatabaseEloquentBuilderTest.php b/tests/Database/DatabaseEloquentBuilderTest.php index 96a6beed7e20..09146044a349 100755 --- a/tests/Database/DatabaseEloquentBuilderTest.php +++ b/tests/Database/DatabaseEloquentBuilderTest.php @@ -1282,6 +1282,7 @@ public function testWhereAttachedTo() { $related = new EloquentBuilderTestModelFarRelatedStub; $related->id = 49; + $related->name = 'test'; $builder = EloquentBuilderTestModelParentStub::whereAttachedTo($related, 'roles'); @@ -1292,9 +1293,11 @@ public function testWhereAttachedToCollection() { $model1 = new EloquentBuilderTestModelParentStub; $model1->id = 3; + $model1->name = 'test3'; $model2 = new EloquentBuilderTestModelParentStub; $model2->id = 4; + $model2->name = 'test4'; $builder = EloquentBuilderTestModelFarRelatedStub::whereAttachedTo(new Collection([$model1, $model2]), 'roles'); From 678b32b941d1fe6f26eb1eaa5616a81e988794ef Mon Sep 17 00:00:00 2001 From: Ashley Shenton Date: Wed, 7 May 2025 09:27:57 +0100 Subject: [PATCH 2/3] test: update whereAttachedTo integration tests to showcase the issue --- tests/Database/DatabaseEloquentIntegrationTest.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/Database/DatabaseEloquentIntegrationTest.php b/tests/Database/DatabaseEloquentIntegrationTest.php index 1d176afa00f1..c27cbfe476b1 100644 --- a/tests/Database/DatabaseEloquentIntegrationTest.php +++ b/tests/Database/DatabaseEloquentIntegrationTest.php @@ -173,6 +173,7 @@ protected function createSchema() $this->schema($connection)->create('achievements', function ($table) { $table->increments('id'); + $table->integer('status')->nullable(); }); $this->schema($connection)->create('eloquent_test_achievement_eloquent_test_user', function ($table) { @@ -1497,7 +1498,7 @@ public function testWhereAttachedTo() $user1 = EloquentTestUser::create(['email' => 'user1@gmail.com']); $user2 = EloquentTestUser::create(['email' => 'user2@gmail.com']); $user3 = EloquentTestUser::create(['email' => 'user3@gmail.com']); - $achievement1 = EloquentTestAchievement::create(); + $achievement1 = EloquentTestAchievement::create(['status' => 3]); $achievement2 = EloquentTestAchievement::create(); $achievement3 = EloquentTestAchievement::create(); @@ -2988,6 +2989,7 @@ class EloquentTestAchievement extends Eloquent public $timestamps = false; protected $table = 'achievements'; + protected $guarded = []; public function eloquentTestUsers() { From 979d59a471684045591cdf4ec8d42e06d26e1b78 Mon Sep 17 00:00:00 2001 From: Ashley Shenton Date: Wed, 7 May 2025 09:28:23 +0100 Subject: [PATCH 3/3] fix: pluck the key from the collection before passing to whereKey --- .../Database/Eloquent/Concerns/QueriesRelationships.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php b/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php index f9f21536d1fc..b7955bd111a9 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php +++ b/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php @@ -803,7 +803,7 @@ public function whereAttachedTo($related, $relationshipName = null, $boolean = ' $this->has( $relationshipName, boolean: $boolean, - callback: fn (Builder $query) => $query->whereKey($relatedCollection), + callback: fn (Builder $query) => $query->whereKey($relatedCollection->pluck($related->getKeyName())), ); return $this;