emmanuelkatto24's avatar

Emmanuel Katto from Dubai : How do I ensure that only the fields with updated information are modified in the database?

Hi, I'm Emmanuel Katto from Duabi, United Arab Emirates (UAE) There is a form in which some fields have the disabled attribute. Using JavaScript, I make individual inputs active and send the form to the controller. There I output it to the dd request. I see that only active inputs arrive, and inputs with the disabled attribute do not arrive. Tell me, is there a way to update the records in the database of only those fields that arrived, so that individual data is not overwritten with empty ones?

The question is not about how to do this, but about whether there is some shorter way in Laravel or in principle? It is clear that you can unload the value and compare

0 likes
1 reply
tykus's avatar

The update method on an Eloquent model instance will take an array of key/value pair with the changes you want to make. Only the values that actually are dirty for that model instance will be written to the database.

If your Request has only the "active inputs" then these values can be written to the database record using

$model->update($request->all())

Aside, I would also recommend validation of incoming changes, but that is off topic for this thread

Please or to participate in this conversation.