How To Easily Delete Multiple Records In Laravel Using Checkbox

When you build an admin panel, you generally need to add a shortcut feature to delete multiple entries in one click. In this blog post, we’ll explain how to delete multiple records using the checkbox in Laravel.

This blog post is a part of a series that introduces you to the Laravel world by building a simple contact form protected by Google ReCaptcha and an admin panel to manage messages sent through our contact form.

In this series, we’re building a simple contact form with an admin panel. To follow this tutorial, download the source we produced in the previous tutorial here.

The list-messages.blade.php view file

To complete this task, we’ll need some JavaScript libraries such as JQuery, and Sweetalert2.

The JQuery library

JQuery logo

JQuery is a small and fast JavaScript library that facilitates the manipulation of DOM(Document Object Model). We’ll include it in our file using the following CDN(Content Delivery Network):

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

The Sweetalert2 library

The Sweetalert2 logo

Sweetalert2 is a beautiful, responsive, and highly customizable JavaScript library with the aim to replace regular JavaScript popup boxes. We’ll use this library to ask for confirmation before proceeding with the deletion of records. It is integrated into our project using the following CDN:

 <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

The JQuery TableCheckAll plugin

The JQuery plugins are the way to expand JQuery functionalities. We are going to use one of them called TableCheckAll to implement our multiple delete feature. To add this plugin to your project, clone the project using the following command:

git clone https://github.com/codeanddeploy/Jquery-Table-Check-All-Plugin.git

Navigate to Jquery-Table-Check-All-Plugin/dist folder and copy the file TableCheckAll.js to our Laravel project into the folder public/js/plugins or where you want in the public folder.

If you have pasted the file in the directory we have indicated above, use the following code to associate that file with our view file.

<script src="{{ asset('js/plugins/TableCheckAll.js') }}"></script>

The csrf-token

We will make a request to delete messages through Ajax request. Each request must have a csrf-token in its header. To be able to use csrf-token in JavaScript, we’ll add it in the header of our file as metadata. The code is the following:


 <meta name="csrf-token" content="{{ csrf_token() }}">

The JavaScript code

The complete script code is the following:

We’ll explain this code in detail.

$(document).ready();

This is a JQuery function that allows us to only execute a code when the page we are on is ready to facilitate our manipulation of DOM. Inside this method, we initialize the JQuery TableCheckAll plugin on the table with the id messages.

After that, we selected the delete all button with their id multiple-delete and add a click event on it. Once the user clicks on it, we retrieve all the id of the row the user has selected and add them to the selected array like show in the code below.

$("#messages .check:checked").each(function() {
      selected.push($(this).val());
 });

From here, the rest of the code is pretty simple to understand. We just used the Sweetalert2 library to ask for confirmation before deleting the records.

The complete code of the file list-messages.blade.php is the following:

The routes/web.php file

It’s time to add the routes to make multiple records deletion. Open the file routes/web.php and add that route as shown in the code below:

Route::middleware(['auth'])->group(function () {
   ...
    Route::post("/admin/messages/multiple-delete", [ContactController::class, 'deleteMultipleMessages'])->name('multiple-delete');
    ...
});

The ContactController.php file

Open the file app/Http/Controllers/ContactController.php and add the following method:

 public function deleteMultipleMessages(Request $request)
 {
    $ids = $request->get('selected');
    Contact::whereIn('id', $ids)->delete();
    return response("Selected messages have been deleted successfully", 200);
}

Here we retrieve the list of the messages id to delete from the request, delete all those messages and return the successful delete message in JSON format.

Testing

Start your development server with the command php artisan serve and try this feature in your admin panel.

Conclusion

In this blog post, we have explained how to implement multiple records deletion in Laravel using the checkboxes in a beautiful and simple way. If you encounter a problem in any step and need more explanation, just leave a comment below. The complete code for this tutorial is found here.

7 thoughts on “How To Easily Delete Multiple Records In Laravel Using Checkbox

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top
porn by force indianhub.me indian girls fuck photos
kannada auntys sex videos ounoun.com indian local sex videos com
kolkata teen sex indianporncave.mobi telugu xxx .com
gotporn redwap3.com indian men naked
m taxi 69 hotindiantube.net incent porn
indian awesome sex tryporn.net xnll
gand porn analpornstars.info xnxx jabardasti
dj-punjab.in xxxvideohd.info mp3 hindi mp3
home sex malayalam pornstarsporn.info indian sex vid
download indian girl sex video indiapornfilm.pro desi xvedios
ndiansex babezporn.com unexpected sex
xvideo plumber xxx-tube-list.net kannada real sex
sexy video rap arabpornsamples.com poonam pandey xvideo
softcore 69 tryporno.net fuck.sc
shemalexnxx screwmyindianwife.info desi choda video