Pass empty fields on the update method using Laravel Request

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?

3 answers

  • answered 2018-07-12 06:19 HCK

    The thing is, Laravel transforms the '' values to null by default through the ConvertEmptyStringsToNull middleware. Check your Kernel:

    app/Http/Kernel.php

    protected $middleware = [
        // some middlewares..
        \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
        // some more middlewares..
    ];
    

    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.

  • answered 2018-07-12 07:11 DolDurma

    You can use php array_merge() like with this code as i use that

    $contentCategory->update(array_merge($request->all(), ["FIELD1" => '',"FIELD2" => '']));
    

    or test this code:

    $request->request->add(['FIELD1' => '','FIELD2' => '']);
    

  • answered 2018-07-12 15:17 Talha Siddiqi

    Input Trimming & Normalization
    Laravel version > 5.4 already has these features included:


    For versions < 5.4: make a new middleware with following code:

    public function handle($request, Closure $next)
    {
        foreach ($request->input() as $key => $value) {
            if (! $value) {
                $request->request->set($key, NULL);
            }
        }
    
        return $next($request);
    }
    

    and assign it to the routes or controller's methods you want it to take place:

    Route::post('/route/{id}', [
       'middleware' => NewMiddleware::class,
    ], function (Request $request, $id) {
        Model::find($id)->update($request->all());
    });
    

    REMEMBER: create(), update(), etc. methods work on fields in $fillable array in your model. If your field is not in the $fillable array in "Model" class, the update() method will not work for that field!!!