Laravel Crud Operation - Delete record

Laravel Crud Operation - Delete record

In this article, we learn about the process of deleting data in the "Laravel crud operation". This is the fourth article on the "Laravel".

Laravel Crud Operation-Delete record


Laravel Crud Operation-Delete record


You can see in the above image that some records show the use name, email, contact, and profile with three buttons update password, update, and delete button.

For deleting the record first create the route with one parameter.

    Route::get('/delete/{id}', [CrudController::class, 'deleterecord'])->name('delete.data');

Route Explanation

This line of code defines a route in a web application, specifically designed for deleting records. In the context of a CRUD (Create, Read, Update, Delete) system, this route is responsible for handling the deletion of a specific record identified by its unique "id."

Route Definition:

The `Route::get()` method is used to define an HTTP GET route. This means that this route is triggered when a user accesses a particular URL using the GET method.

URL Structure

The route is set to respond to the URL path "/delete/{id}". Here, "{id}" is a placeholder for a unique identifier associated with the record to be deleted. Users can navigate to this URL to initiate the deletion process.

Controller and Method:

The route is associated with a controller method through `[CrudController::class, 'deleterecord']`. This indicates that when the specified URL is accessed, the 'deleterecord' method within the 'CrudController' class will be executed to handle the deletion logic.

Route Name:

The `->name('delete.data')` part assigns a name to the route, allowing developers to reference it by this name in their code. In this case, the route is named 'delete.data.'

After route are created We will create a new function into the controller file.

    public function deleterecord(request $request, $id)
    {
        $delete_data = CrudModel::find($id);
        $delete_data->delete();
        if($delete_data)
        {
            return redirect()->back()->with('success', 'Record Has Been Successfully Delete.');
        }
        else
        {
            return redirect()->back()->with('error', 'Record Has Not Been Successfully Delete');
        }
    }

Explain the Controller Code

Function Declaration:

    public function deleterecord(request $request, $id)
    {

This declares a public function named deleterecord, which takes two parameters: $request and $id. The $request parameter is of type request, indicating it likely contains HTTP request data, and $id is a variable representing the identifier of the record to be deleted.

Retrieve Record for Deletion:

    $delete_data = CrudModel::find($id);

This line uses the CrudModel to find a record with the specified $id. The result is stored in the variable $delete_data.

Delete Record:

    $delete_data->delete();

It calls the delete method on the retrieved record ($delete_data), effectively deleting the record from the database.

Conditional Redirect:

    if($delete_data)
    {
        return redirect()->back()->with('success', 'Record Has Been Successfully Delete.');
    }
    else
    {
        return redirect()->back()->with('error', 'Record Has Not Been Successfully Delete');
    }

This block checks if the deletion was successful. If $delete_data exists (meaning the record was found and deleted), it redirects the user back to the previous page with a success message. Otherwise, it redirects with an error message.

Laravel Crud Operation-Delete record


Post a Comment

Previous Post Next Post

Recent in Technology