8000 [5.8] Less Greedy Mutator by browner12 · Pull Request #29392 · laravel/framework · GitHub
[go: up one dir, main page]

Skip to content

[5.8] Less Greedy Mutator #29392

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

Closed
wants to merge 2 commits into from
Closed
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
8 changes: 6 additions & 2 deletions src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -568,13 +568,17 @@ public function setAttribute($key, $value)
// which simply lets the developers tweak the attribute as it is set on
// the model, such as "json_encoding" an listing of data for storage.
if ($this->hasSetMutator($key)) {
return $this->setMutatedAttributeValue($key, $value);
$value = $this->setMutatedAttributeValue($key, $value);

if (is_null($value)) {
return $value;
}
}

// If an attribute is listed as a "date", we'll convert it from a DateTime
// instance into a form proper for storage on the database tables using
// the connection grammar's date format. We will auto set the values.
elseif ($value && $this->isDateAttribute($key)) {
if ($value && $this->isDateAttribute($key)) {
$value = $this->fromDateTime($value);
}

Expand Down
13 changes: 13 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ public function testAttributeManipulation()
$this->assertEquals(json_encode(['name' => 'taylor']), $attributes['list_items']);
}

public function testCastAttributeCanBeMutated()
{
$model = new EloquentModelStub;
$model->colors = ['green', ' red', 'blue ', ' orange ', ' p ink '];
$this->assertEquals(['green', 'red', 'blue', 'orange', 'p ink'], $model->colors->all());
}

public function testDirtyAttributes()
{
$model = new EloquentModelStub(['foo' => '1', 'bar' => 2, 'baz' => 3]);
Expand Down Expand Up @@ -1952,6 +1959,7 @@ class EloquentModelStub extends Model
protected $table = 'stub';
protected $guarded = [];
protected $morph_to_stub_type = EloquentModelSaveStub::class;
protected $casts = ['colors' => 'collection'];

public function getListItemsAttribute($value)
{
Expand All @@ -1973,6 +1981,11 @@ public function setPasswordAttribute($value)
$this->attributes['password_hash'] = sha1($value);
}

public function setColorsAttribute($value)
{
return array_map('trim', $value);
}

public function publicIncrement($column, $amount = 1, $extra = [])
{
return $this->increment($column, $amount, $extra);
Expand Down
0