2

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?

2
  • they aren't stripped out, if the browser submitted it, they are there. Commented Jul 12, 2018 at 6:04
  • what do you get when you var_dump($request->all())? Commented Jul 12, 2018 at 16:03

4 Answers 4

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

My problem is that I don't get the request input at all
That is just because of this. If the input value is a null Laravel will 'ignore' its existence. If you comment that line it will keep the '' values.. ergo, this attributes will be present in your request.
On my both web application and API ConvertEmptyStringsToNull middleware is active (not commented), yet when I log the request on my web application I can see empty fields with null value and on my API I don't see them at all. Why is that?
That middleware only changes the value to an explicit null value. The inputs are still there, they are not 'ignored'.
1

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:

class User extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['name', 'email', 'contact', ];
}

Or if you want to exclude some fields and include rest, use $guarded array instead:

class User extends Model
{
    /**
     * The attributes that aren't mass assignable.
     *
     * @var array
     */
    protected $guarded = ['details'];
}

If your field is not in the $fillable array in "Model" class, the update() method will not work for that field!!!

Reference: Mass Assignment

Comments

0

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' => '']);

Comments

0

Somehow I managed to make it work: on my web application I have disabled ConvertEmptyStringsToNull middleware (commented it) so when I log the $request I get empty values '' and on the API, $request logs null value for each field.

1 Comment

was not this what I recommend you to do?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.