Home Ask Login Register

Developers Planet

Your answer is one click away!

karmendra February 2016

laravel update shouldn't do anything if no value changed

If I open a form to edit and without changing any value just hit Submit button to update, it updates the row in the table i.e. it changes the updated_at time and even executes some code in bootXXXX(static::saving(function($model) {}) function.

I do not want that to happen.

How can I find If user changed nothing and its a blank submit button hit?

Of course before update I can fetch the current values and compare them with input $request->all() and decide to update or not, but Is there any better way to do so the laravel way?

Thanks, K


Imtiaz Pabel February 2016

you have 2 methods one is getDirty() by using this you will get current changed input value, another one is getOriginal() if you use that you will get what previous data you have

By using those 2 methods i am giving here a example,may be it will help you

foreach($model->getDirty() as $attribute => $value){
    $original= $model->getOriginal($attribute);
    echo "Changed $attribute from '$original' to '$value'<br/>\r\n";
return true; //if false the model wont save! 

Post Status

Asked in February 2016
Viewed 3,892 times
Voted 7
Answered 1 times


Leave an answer

Quote of the day: live life