Skip to content

Commit 045bc79

Browse files
authored
Allow mass assigmnet with mutators when using guard (#52962)
1 parent c059d5d commit 045bc79

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ public function isGuarded($key)
214214
*/
215215
protected function isGuardableColumn($key)
216216
{
217+
if ($this->hasSetMutator($key) || $this->hasAttributeSetMutator($key)) {
218+
return true;
219+
}
220+
217221
if (! isset(static::$guardableColumns[get_class($this)])) {
218222
$columns = $this->getConnection()
219223
->getSchemaBuilder()
@@ -222,6 +226,7 @@ protected function isGuardableColumn($key)
222226
if (empty($columns)) {
223227
return true;
224228
}
229+
225230
static::$guardableColumns[get_class($this)] = $columns;
226231
}
227232

tests/Database/DatabaseEloquentModelTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3143,6 +3143,32 @@ public function testModelToJsonSucceedsWithPriorErrors(): void
31433143

31443144
$this->assertSame('{"name":"Mateus"}', $user->toJson(JSON_THROW_ON_ERROR));
31453145
}
3146+
3147+
public function testFillableWithMutators()
3148+
{
3149+
$model = new EloquentModelWithMutators;
3150+
$model->fillable(['full_name', 'full_address']);
3151+
$model->fill(['id' => 1, 'full_name' => 'John Doe', 'full_address' => '123 Main Street, Anytown']);
3152+
3153+
$this->assertNull($model->id);
3154+
$this->assertSame('John', $model->first_name);
3155+
$this->assertSame('Doe', $model->last_name);
3156+
$this->assertSame('123 Main Street', $model->address_line_one);
3157+
$this->assertSame('Anytown', $model->address_line_two);
3158+
}
3159+
3160+
public function testGuardedWithMutators()
3161+
{
3162+
$model = new EloquentModelWithMutators;
3163+
$model->guard(['id']);
3164+
$model->fill(['id' => 1, 'full_name' => 'John Doe', 'full_address' => '123 Main Street, Anytown']);
3165+
3166+
$this->assertNull($model->id);
3167+
$this->assertSame('John', $model->first_name);
3168+
$this->assertSame('Doe', $model->last_name);
3169+
$this->assertSame('123 Main Street', $model->address_line_one);
3170+
$this->assertSame('Anytown', $model->address_line_two);
3171+
}
31463172
}
31473173

31483174
class EloquentTestObserverStub
@@ -3870,3 +3896,35 @@ protected function stepOut(): void
38703896
}
38713897
}
38723898
}
3899+
3900+
class EloquentModelWithMutators extends Model
3901+
{
3902+
public $attributes = [
3903+
'first_name' => null,
3904+
'last_name' => null,
3905+
'address_line_one' => null,
3906+
'address_line_two' => null,
3907+
];
3908+
3909+
protected function fullName(): Attribute
3910+
{
3911+
return Attribute::make(
3912+
set: function (string $fullName) {
3913+
[$firstName, $lastName] = explode(' ', $fullName);
3914+
3915+
return [
3916+
'first_name' => $firstName,
3917+
'last_name' => $lastName,
3918+
];
3919+
}
3920+
);
3921+
}
3922+
3923+
public function setFullAddressAttribute($fullAddress)
3924+
{
3925+
[$addressLineOne, $addressLineTwo] = explode(', ', $fullAddress);
3926+
3927+
$this->attributes['address_line_one'] = $addressLineOne;
3928+
$this->attributes['address_line_two'] = $addressLineTwo;
3929+
}
3930+
}

0 commit comments

Comments
 (0)