Arnaud LiDz
Arnaud LiDz

Reputation: 311

Laravel 5.5 - Check if only one column will be updated

Using Laravel 5.5 and models, I would like to know, before updated if only one column, statut, will be updated.

The interest is to just updated the statut if there are no changes or, if there are some columns to be updated, create a new row. Because each modification need to be approved before published.

Actually, I have a big if like :

if($request->statut != $product->statut && $request->title == $product ...)

Is there a quickest way ?

Thanks.

Upvotes: 0

Views: 540

Answers (1)

d3jn
d3jn

Reputation: 1410

You can utilize isDirty('field') method to check if the field was changed (and not yet saved). getDirty() will return you array of all such fields and their values.

If I understood your case correctly, then you can do:

if ($product->isDirty('statut')) {
    if (count($product->getDirty()) == 1) {
        ...
    }
}

First we check if field statut was changed for your $product. Then we just check if number of changed fields is 1 (meaning that there are no more changed fields than just statut).

Upvotes: 1

Related Questions