Unexpected behavior while trying to fill
and save
a non-db property
#57102
Replies: 4 comments
-
Can you share your mutator's body? A mutator should return either a value or an associative array with the columns to be updated as keys. In your case, I assume you don't want any columns to be updated, as it seems to be a convenience attribute to ease the relation access, right? But every function in PHP returns something. Even when no explicit return is given, or even when a function or method has a return type of Reference: https://www.php.net/manual/en/language.types.void.php To tell Eloquent the setter should not touch any columns in the database, you can return an empty array: public function tags(): Attribute
{
return Attribute::make(
get: fn () => $this->categories()->pluck('name'),
set: function ($value) {
$this->categories()
->sync(Category::query()->whereIn('name', $value)->pluck('id'));
// no columns should be updated
return [];
},
);
} |
Beta Was this translation helpful? Give feedback.
-
So, my main question is to understand why the I did not get it, I thought the new |
Beta Was this translation helpful? Give feedback.
-
On the test app I made for this, it is called when I added a public function tags(): Attribute
{
return Attribute::make(
get: fn () => $this->categories()->pluck('name'),
set: function ($value) {
\dump([__METHOD__ => $value]);
$this->categories()->sync(Category::query()->whereIn('name', $value)->pluck('id'));
// no columns should be updated
return [];
},
);
} The calling code is like this: <?php
use Illuminate\Support\Facades\Artisan;
Artisan::command('app:test', function () {
$task = \App\Models\Task::query()->first();
$task->fill([
'tags' => ['Lime', 'Pink'],
])->save();
\dump($task->fresh('categories')->tags);
}); And this is the output: $ php artisan app:test
array:1 [
"App\Models\{closure}" => array:2 [
0 => "Lime"
1 => "Pink"
]
] // app/Models/Task.php:29
Illuminate\Support\Collection^ {#957
#items: array:2 [
0 => "Pink"
1 => "Lime"
]
#escapeWhenCastingToString: false
} // routes/console.php:12 You can see the first dump is from the model's mutator. I uploaded the test app to a public repository in case you want to share what is done differently: |
Beta Was this translation helpful? Give feedback.
-
Thank you for take the time to do this test Rodrigo, I'll retry in a small version of my current scenario as you did and review from there. I'll report my findings here. For more context, I'm on greenfield, my project setup is laravel 12.x, filament 4.x, php 8.4, tailwind 4.x. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Laravel Version
12
PHP Version
8.4
Database Driver & Version
SQLite
Description
I might be missing something but today I noticed something weird while trying to
fill
andsave
an object where thefillable
attribute is amutator
.Steps To Reproduce
This works fine:
This throws an error onSave/Update:
Error message:
What am I missing? Why the
Attribute->set
behavior is different from the old fashion mutator?Beta Was this translation helpful? Give feedback.
All reactions