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; 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'); 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() {