Eloquent explicitly rejects fields/columns starting with an underscore
It was introduced here
https://github.com/laravel/framework/commit/1000e2dca7f42d5cd5e03cbdc85fcf957b7a5548
It seems the only way around this is to manually specify each underscore column as fillable or set unguarded to be true.
Is there any reason for this ? Only reasons I can think would be a system where prefixing _
means a private variable and not put in the to DB ? Seems odd when underscore is a valid column prefix in SQL
I think it is because Eloquent uses magic. As it does, it must enforce conventions on naming to ensure no unnecessary bugs appear.
For instance, take a look at the code of Eloquent\Model\Concerns\HasAttributes and you will see it using
Str::studly
to define mutators.If Eloquent allows defining
foo_column
and a_foo_column
, both will have the same mutators functionsgetFooColumn
. This can lead to many problems. For example, defining a mutator forfoo_column
will shadow_foo_column
also.