Skip to content

Commit

Permalink
Add deleteOrFail to Model (laravel#38784)
Browse files Browse the repository at this point in the history
Makes it more convenient to handle delete errors in destroy actions.
  • Loading branch information
caugner authored Sep 13, 2021
1 parent c42c356 commit a228123
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,24 @@ public function delete()
return true;
}

/**
* Delete the model from the database within a transaction.
*
* @return bool|null
*
* @throws \Throwable
*/
public function deleteOrFail()
{
if (! $this->exists) {
return false;
}

return $this->getConnection()->transaction(function () {
return $this->delete();
});
}

/**
* Force a hard delete on a soft deleted model.
*
Expand Down

0 comments on commit a228123

Please sign in to comment.