Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel how to update value in mysql only if value is changed

I'm new to Laravel. All I'm trying to do is as follow:

I have some fields in my form like TITLE, DESCRIPTION.

TITLE field is unique in database.

This is what I've done to update my values.

$product              = Product::find($id); 
$product->title       = $request->title;
$product->description = $request->description;
$product->save();

But this will give error (that value already exists) as my TITLE field is unique.

All I want to is update my TITLE only if the TITLE value is changed otherwise update the same value but update other fields. Thanks

like image 775
nasor Avatar asked Dec 09 '16 06:12

nasor


1 Answers

Something like this?

$product = Product::find($id);

if ($product->title == $request->title) {
    $product->description = $request->description;
} else {
    $product->title = $request->title;
}

$product->save();
like image 52
Alexey Mezenin Avatar answered Nov 15 '22 00:11

Alexey Mezenin