nasor
nasor

Reputation: 441

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

Upvotes: 5

Views: 7824

Answers (5)

Zeeshan Chawdhary
Zeeshan Chawdhary

Reputation: 60

You should look at wasChanged() function in Laravel.

Upvotes: 0

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21681

You can use if statement to check weather column value is same with original?

$currentProduct         = Product::find($id);
//Check and update if title not same
if($currentProduct->title == $request->title){
    $currentProduct->description   = $request->description;   
}
//Update title and description...
else {
    $currentProduct->title         = $request->title;
    $currentProduct->description   = $request->description;
} 

$currentProduct->save();

Upvotes: 0

LSerni
LSerni

Reputation: 57408

The code you posted does exactly (if indirectly) what you say you want. Your problem is another: you're assigning a title that is used elsewhere, thereby violating uniqueness.

You must verify that the new title is not already used.

$duplicate = Product::where('title', '=', $request->title)->where('id', '!=', $id)->first();

At this point it is unclear what you want to do:

a) Do you want to use the duplicate record instead of the one indicated by $id?

b) Is this an error?

c) Should the $id record be updated, leaving the title alone?

You can do any of these things; what you can't do is "update my TITLE only if the TITLE value is changed", because the changed value is already in use elsewhere.

Upvotes: 1

Alexey Mezenin
Alexey Mezenin

Reputation: 163808

Something like this?

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

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

$product->save();

Upvotes: 6

Birdy
Birdy

Reputation: 773

I have not seen any of your database structure or form code only the small snippet above you have stated.

you could do something like this...

$checkTitle = Product::where('title', '=', $request->input('title')->first();

You can also do...

   $record = Product::find($id)

    if($request->input('title') === $record->title){
     // Record with the $id has the same title as you are posting in the request.
    }else{
         // Title for $id lets say id number 3, Is not the same as the form request you are sending so now Perform insert
      }


   if(count($checkTitle) >= 1){
      // Means the title exists and do what ever you want...
     //  Perform update
    }else{
      // Perform insert
     }

Sorry for short and sweet, leaving to go to office and thought it might help to post now. Let me know if im on the wrong tracks and ill help where i can.

Upvotes: 0

Related Questions