8000 [12.x] Only pass model IDs to Eloquent `whereAttachedTo` method by ashleyshenton · Pull Request #55666 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[12.x] Only pass model IDs to Eloquent whereAttachedTo method #55666

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions tests/Database/DatabaseEloquentBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1282,6 +1282,7 @@ public function testWhereAttachedTo()
{
$related = new EloquentBuilderTestModelFarRelatedStub;
$related->id = 49;
$related->name = 'test';

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

Expand All @@ -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');

Expand Down
4 changes: 3 additions & 1 deletion tests/Database/DatabaseEloquentIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -2988,6 +2989,7 @@ class EloquentTestAchievement extends Eloquent
public $timestamps = false;

protected $table = 'achievements';
protected $guarded = [];

public function eloquentTestUsers()
{
Expand Down
0