Skip to content

Commit

Permalink
Simplify where-ing in withAttributes
Browse files Browse the repository at this point in the history
  • Loading branch information
tontonsb committed Nov 30, 2024
1 parent f8874ed commit 6ff8474
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
6 changes: 1 addition & 5 deletions src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,11 +535,7 @@ public function withAttributes(array|string|Expression $attributes, $value = nul
}

foreach ($attributes as $column => $value) {
if (is_null($value)) {
$this->whereNull($column);
} else {
$this->where($column, $value);
}
$this->where($column, $value);
}

$this->pendingAttributes = array_merge($this->pendingAttributes, $attributes);
Expand Down
24 changes: 24 additions & 0 deletions tests/Database/DatabaseEloquentHasOneOrManyWithAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,30 @@ public function testWheresAreSet(): void
], $wheres);
}

public function testNullValueIsAccepted(): void
{
$parentId = 123;
$key = 'a key';

$parent = new RelatedWithAttributesModel;
$parent->id = $parentId;

$relationship = $parent
->hasMany(RelatedWithAttributesModel::class, 'parent_id')
->withAttributes([$key => null]);

$wheres = $relationship->toBase()->wheres;
$relatedModel = $relationship->make();

$this->assertNull($relatedModel->$key);

$this->assertContains([
'type' => 'Null',
'column' => $key,
'boolean' => 'and',
], $wheres);
}

public function testOneKeepsAttributesFromHasMany(): void
{
$parentId = 123;
Expand Down

0 comments on commit 6ff8474

Please sign in to comment.