laravel Eloquent ORM delete() method

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Understanding Laravel Eloquent ORM delete() Method and Alternative Approaches for Handling Deletion Responses Introduction When working with databases in Laravel, one of the most important aspects to understand is how data is deleted using the Laravel Eloquent Object-Relational Mapping (ORM) library. In this blog post, we will discuss the delete() method of the Eloquent model and provide solutions for handling different results when deleting database records. Understanding the delete() Method Let's first explore the delete() method within Laravel's Eloquent ORM. This method is used to permanently remove a record from the database, including handling any dependencies between related models. It returns true if successfully deleted or false if there was an issue during deletion. Here's the code snippet:
$res = User::find($id)->delete();
if ($res) {
    $data = [
        'status' => '1',
        'msg' => 'success'
    ];
} else {
    $data = [
        'status' => '0',
        'msg' => 'fail'
    ];
return response()->json($data);
In this example, the delete() method is called on a User model instance found with the given ID. The resulting boolean value is then checked and used to determine whether the deletion was successful or not. If everything went well, a JSON response containing the status of '1' (true) and 'success' message is returned. Otherwise, a response with status '0' (false) and 'fail' message is sent back to the user. Dealing with Null Responses Sometimes, when using a null result from the delete() method, it can seem puzzling as the return value should be either true or false. There are several reasons why this might occur: 1. The record you're trying to delete doesn't exist in your database. This could happen if there was an error during data retrieval or the user entered an invalid ID. In this case, you might want to validate and double-check the provided ID before proceeding with the deletion operation. 2. The record hasn't been found by the find() method but Eloquent is not throwing any exceptions. This could happen when working with models that don't extend the Model class or when using custom primary keys, which are handled differently than the default integer primary key. In these situations, it is recommended to use try-catch blocks in order to handle errors effectively. 3. The record couldn't be deleted due to a dependency issue (foreign key constraints). This happens when the current model has relationships with other models and the relationship must follow specific deletion rules before it can delete its own records. In such cases, Laravel will throw an exception specifying the error code and reason. Alternative Approaches for Handling Deletion Results A more straightforward approach to handling the deletion response would be using the try-catch block as follows:
try {
    $res = User::find($id)->delete();
} catch (\Exception $e) {
    if ($e->getCode() === 23503) { // Foreign key constraint violation error code
        $data = [
            'status' => '0',
            'msg' => 'foreign_key_constraint_violation'
        ];
    } else if ($e instanceof \Illuminate\Database\QueryException) {
        $data = [
            'status' => '0',
            'msg' => $e->getMessage()
        ];
    } else {
        $data = [
            'status' => '0',
            'msg' => 'unknown_error'
        ];
    }
return response()->json($data);
In this code snippet, we first try to delete the user record and handle the result. If an exception is thrown during the deletion process (either a foreign key constraint violation or any other database error), we parse the error message and provide a custom response depending on the type of error encountered. This approach ensures that you respond with an appropriate message for each specific deletion issue while maintaining the same format as before. Conclusion The Laravel Eloquent ORM delete() method is essential for safely removing data from your database, providing a way to handle issues such as missing or non-existing records and foreign key constraints. By understanding the possible outcomes of this method and implementing alternative approaches, you can ensure that your Laravel applications respond efficiently and effectively when dealing with database deletion operations.