Skip to main content

How to Check If Record Is Created or Updated Successful in Laravel?

If you’re using Laravel, and need to know if a record has been created or updated successfully, it can be done pretty easily. Laravel Eloquent is an ORM or object-relational mapper. It provides a mechanism for mapping between PHP objects and database tables. Eloquent is used to simplify working with databases by providing a set of classes that map directly to database tables.

It makes the task of checking where a record is updated or created a breeze as well. Here is a way to check if a record has been created successfully in Laravel.

//check if record was created succesfully
$message = Message::create($data);
if($message->exists){
	echo 'created';
} else {
	echo 'failed';
}
//check if record was updated succesfully
$affectedRows = Message::where('id', 12)->update(["body" => "Updated body"]);
if($affectedRows > 0){
	echo 'updated';
} else {
	echo 'failed';
}

By continuing to use the site, you agree to the use of cookies.