Deleting Multiple Record in Laravel | websolutioncode.com
Deleting Multiple Record in Laravel | websolutioncode.com

How to Deleting Multiple Record in Laravel

Introduction:

Deleting multiple record in Laravel can be a common task when working with databases. Whether you’re building a content management system, an e-commerce platform, or any other web application, there may come a time when you need to remove multiple records efficiently. In this tutorial, we’ll walk through the process of deleting multiple records in Laravel, providing you with practical code examples and explanations.

Requirements:

Before we begin, make sure you have the following prerequisites:

  1. Laravel installed on your machine.
  2. A working database connection in your Laravel project.

Step 1: Create a Model:

Assuming you already have a model for the records you want to delete, let’s proceed with creating a model if you don’t have one. Run the following Artisan command in your terminal:

php artisan make:model YourModelName

Replace “YourModelName” with the actual name of your model.

Step 2: Create a Controller:

Next, let’s create a controller to handle the deletion of Deleting Multiple Record in Laravel. Run the following command:

php artisan make:controller YourControllerName

Replace “YourControllerName” with the desired name for your controller.

Step 3: Implement the Controller Logic:

Open the generated controller file (located in the app/Http/Controllers directory) and add the following methods to handle the deletion:

<?php

namespace App\Http\Controllers;

use App\Models\YourModelName;
use Illuminate\Http\Request;

class YourControllerName extends Controller
{
    public function deleteMultiple(Request $request)
    {
        $ids = $request->input('ids');
        
        YourModelName::whereIn('id', $ids)->delete();

        return response()->json(['message' => 'Records deleted successfully']);
    }
}

This code assumes that you are passing an array of record IDs through a POST request to the deleteMultiple method. Adjust the code according to your requirements.

Step 4: Set Up Routes:

Now, let’s define a route for the new controller method. Open the routes/web.php file and add the following:

use App\Http\Controllers\YourControllerName;

Route::post('/delete-multiple', [YourControllerName::class, 'deleteMultiple']);

This route will handle the deletion of multiple records.

Step 5: Create a Blade View (Optional):

If you want to create a user interface for deleting multiple records, you can create a Blade view with a form. Here’s a basic example:

<form method="post" action="{{ url('/delete-multiple') }}">
    @csrf
    <input type="checkbox" name="ids[]" value="1"> Record 1<br>
    <input type="checkbox" name="ids[]" value="2"> Record 2<br>
    <!-- Add more checkboxes as needed -->

    <button type="submit">Delete Selected</button>
</form>

Customize the form according to your model and requirements.

Conclusion:

By following these steps, you’ve learned how to delete multiple records in Laravel efficiently. This approach is suitable for scenarios where you need to remove multiple records based on their IDs. Feel free to adapt the code to your specific use case and improve the user interface based on your application’s requirements.

Check our tools website Word count
Check our tools website check More tutorial

Leave a Reply