I'm getting data through a classic submitted form but for some reason I don't get all the input's data in request and it only updates the received ones.
Model::find($id)->update($request->all());
This is a bit problematic because I have multiple nullable fields in my database and sometimes I want to pass empty fields (i.e when a user clear an input then submit the form)
Do you know a way to pass empty fields to the Request as well?
The thing is, Laravel transforms the ''
values to null
by default through the ConvertEmptyStringsToNull middleware. Check your Kernel:
app/Http/Kernel.php
protected $middleware = [
// ...
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
// ...
];
This middleware do the following:
protected function transform($key, $value)
{
return is_string($value) && $value === '' ? null : $value;
}
As you can see, if the given attribute has a value ''
it will be transformed to null
.
So, to avoid this you could just comment that middleware in the Kernel file. But be aware of this, because this will be applied to your entire application.