Laravel Update Return Value

Laravel Update Return Value

When updating records in Laravel using the Eloquent ORM, the update method returns the number of affected rows. This can be useful to determine if the update operation was successful or not.

Here is an example:

    
      $affectedRows = User::where('age', '>', 30)->update(['active' => 1]);
      
      if ($affectedRows > 0) {
        echo "Update operation successful";
      } else {
        echo "No records updated";
      }
    
  

In this example, we are updating the active column to 1 for all users whose age is greater than 30. The update method returns the number of affected rows, which is stored in the $affectedRows variable.

We then use an if statement to check if any records were updated. If the $affectedRows variable is greater than 0, we display the message “Update operation successful”. Otherwise, if no records were updated, we display the message “No records updated”.

By using the return value of the update method, you can easily handle cases where the update operation fails or when there are no records updated.

Read more

Leave a comment