8000 [12.x] Only pass model IDs to Eloquent `whereAttachedTo` method (#55666) · laravel/framework@30d92c9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 30d92c9

Browse files
[12.x] Only pass model IDs to Eloquent whereAttachedTo method (#55666)
* test: update whereAttachedTo builder tests to showcase the issue * test: update whereAttachedTo integration tests to showcase the issue * fix: pluck the key from the collection before passing to whereKey
1 parent 99debba commit 30d92c9

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ public function whereAttachedTo($related, $relationshipName = null, $boolean = '
803803
$this->has(
804804
$relationshipName,
805805
boolean: $boolean,
806-
callback: fn (Builder $query) => $query->whereKey($relatedCollection),
806+
callback: fn (Builder $query) => $query->whereKey($relatedCollection->pluck($related->getKeyName())),
807807
);
808808

809809
return $this;

tests/Database/DatabaseEloquentBuilderTest.php

+3
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,7 @@ public function testWhereAttachedTo()
12821282
{
12831283
$related = new EloquentBuilderTestModelFarRelatedStub;
12841284
$related->id = 49;
1285+
$related->name = 'test';
12851286

12861287
$builder = EloquentBuilderTestModelParentStub::whereAttachedTo($related, 'roles');
12871288

@@ -1292,9 +1293,11 @@ public function testWhereAttachedToCollection()
12921293
{
12931294
$model1 = new EloquentBuilderTestModelParentStub;
12941295
$model1->id = 3;
1296+
$model1->name = 'test3';
12951297

12961298
$model2 = new EloquentBuilderTestModelParentStub;
12971299
$model2->id = 4;
1300+
$model2->name = 'test4';
12981301

12991302
$builder = EloquentBuilderTestModelFarRelatedStub::whereAttachedTo(new Collection([$model1, $model2]), 'roles');
13001303

tests/Database/DatabaseEloquentIntegrationTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ protected function createSchema()
173173

174174
$this->schema($connection)->create('achievements', function ($table) {
175175
$table->increments('id');
176+
$table->integer('status')->nullable();
176177
});
177178

178179
$this->schema($connection)->create('eloquent_test_achievement_eloquent_test_user', function ($table) {
@@ -1497,7 +1498,7 @@ public function testWhereAttachedTo()
14971498
$user1 = EloquentTestUser::create(['email' => 'user1@gmail.com']);
14981499
$user2 = EloquentTestUser::create(['email' => 'user2@gmail.com']);
14991500
$user3 = EloquentTestUser::create(['email' => 'user3@gmail.com']);
1500-
$achievement1 = EloquentTestAchievement::create();
1501+
$achievement1 = EloquentTestAchievement::create(['status' => 3]);
15011502
$achievement2 = EloquentTestAchievement::create();
15021503
$achievement3 = EloquentTestAchievement::create();
15031504

@@ -2988,6 +2989,7 @@ class EloquentTestAchievement extends Eloquent
29882989
public $timestamps = false;
29892990

29902991
protected $table = 'achievements';
2992+
protected $guarded = [];
29912993

29922994
public function eloquentTestUsers()
29932995
{

0 commit comments

Comments
 (0)
0