How to check PHP if record in db is deleted?
Most of the answers are way complex or using an extra function to call. Laraval delete function returns boolean or null
/**
* Delete the model from the database.
*
* @return bool|null
* @throws \Exception
*/
public function delete()
{
....
....
}
So you can just use this code to check for deletion.
try {
if ($this->delete('/api/posts/1')) {
}
} catch (\Exception $exception) {}
source to share
You can check it with Post::findOrFail()
, or you can just check it out if(Post::findById($id) == null)
.
Edit: Not sure if this still works in Laravel> 5.2, but you can get the collection withTrashed()
if you softly delete the entry. Then you can use if($record->isTrashed()) { ... }
. This function is not used by default in the model, but is present in the characteristic SoftDelete
.
Here you will find information for editing: link .
source to share