framework: Json keys in $fillable and $casts are ignored
Laravel Version
11.34.2
PHP Version
8.3.10
Database Driver & Version
MySQL 8.0
Description
class MyModel extends Model
{
protected $fillable = [
'name',
'data->some_value'
];
}
$model = MyModel::create([
'name' => 'Some name',
'data' => ['some_value' => 1]
]);
//got `null` (`$fillable` ignored)
dd($model->data);
Same situation with $casts:
class MyModel extends Model
{
protected $fillable = [
'name',
'data'
//'data->custom_value' (the presence or absence of this does not matter here)
];
protected function casts(): array
{
return [
//'data' => 'json', (the presence or absence of this does not matter here)
'data->custom_value' => 'bool'
];
}
}
$model = MyModel::create([
'name' => 'Some name',
'data' => ['custom_value' => 1]
]);
//got `1`, not `true` (`$casts` ignored)
dd($model->data->custom_value);
Steps To Reproduce
- Fresh install laravel;
- Add
datafield asjson; - Try to create any model.
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 19 (6 by maintainers)
@projct1 the above is called in \Illuminate\Database\Eloquent\Model::fill
Hi there,
Thanks for reporting the problem you are encountering, but it looks like this is a question which may be better suited for a support channel. We only use this issue tracker for reporting bugs with the library itself. If you have a question on how to use functionality provided by this repository you can try one of the following channels:
However, this issue will not be locked and everyone is still free to discuss solutions to your problem!
Thanks.
Done the check the first time you send the issue, inspected the Laravel Framework code, looks at existing tests and everything are working as expected.
Hi there,
Thanks for reporting the problem you are encountering, but it looks like this is a question which may be better suited for a support channel. We only use this issue tracker for reporting bugs with the library itself. If you have a question on how to use functionality provided by this repository you can try one of the following channels:
However, this issue will not be locked and everyone is still free to discuss solutions to your problem!
Thanks.
Not a bug. This is not the place to ask for support. Issue should be used only to send in confirmed bug.
The above casts format is not supported by Laravel, please create custom Cast class if you need this: https://laravel.com/docs/11.x/eloquent-mutators#anonymous-cast-classes