This post covered topic on Laravel tutorial for beginner step by step, and in this post we have shared simple feature like How to Delete multiple data from Mysql table via Checkbox in Laravel framework with Ajax. In this tutorial i will show you step by step how to delete multiple rows using checkbox in laravel. It is a common thing that delete multiple checkbox value in laravel. For do this operation we will use jQuery Ajax and HTML Check boxes input fields for delete multiple rows of mysql data in Laarvel. Mainly we have always put delete button at individual row for delete single data.
But if we have large amount of data, from that data we want to remove many unwanted rows of data at the same time. Then at that time this functionality will helpful. This is because if we have remove single data one by one then it will take much more time, and by using this feature we can delete bulk data at the same time.
In this example i will discuss about how to delete multiple rows using checkbox in jquery ajax. But i have already did this same tutorial to delete multiple records using vuejs. So you can read this below tutorial also. If we have use Laravel framework for our web development, then we will make any large application by using this framework. So, if application is large, then in that application data will be bulk of amount.
So, If we have made this functionality in our application then it will reduce our work time, and it will increase efficiency of our web application. For make this feature we have use jQuery, Ajax and HTML check boxes. HTML check boxes used for select multiple row of data by selecting checkbox. Ajax used for send and received request to server. By using Ajax with laravel we can delete multiple or bulk of mysql data without refresh of web page. jQuery is used for give animation effect at the time of delete of multiple data.
In this example, i simply created "products" table with id, name, details, created_at and updated_at columns. I also added mysql query for add dummy records. Here i use jquery for select all checkboxs and delete all records. So finally you have to follow some step and get the layout like as bellow.
So let's start our brand new delete row using ajax in laravel tutorial.
Step 1: Create products Table
Here, you have to create "products" table then you can run mysql query for dummy records. You can create products table using migration and then also create some dummy records using seeder.
Step 2: Create new Routes
In this step, we are doing from scratch so we will add three routes, one for display data and another for delete request, then third for remove all selected data. So you have to simply add three new routes in your laravel application.
routes/web.php
Route::get('myproducts', 'ProductController@index');
Route::delete('myproducts/{id}', 'ProductController@destroy');
Route::delete('myproductsDeleteAll', 'ProductController@deleteAll');
Step 3: Add ProductController
Here, we will create new ProductController file to handle request of created three new route. In this Controller we define three method, index(), destroy() and deleteAll(). method will handle route request. So let's create new controller and put code:
Read Also: Laravel Gate and Policy Example from Scratch
app/Http/Controllers/ProductController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProductController extends Controller
{
public function index()
{
$products = \DB::table("products")->get();
return view('products',compact('products'));
}
public function destroy($id)
{
\DB::table("products")->delete($id);
return response()->json(['success'=>"Product Deleted successfully.", 'tr'=>'tr_'.$id]);
}
public function deleteAll(Request $request)
{
$ids = $request->ids;
\DB::table("products")->whereIn('id',explode(",",$ids))->delete();
return response()->json(['success'=>"Products Deleted successfully."]);
}
}
Step 4: Add Blade File
In last step, we will create products.blade.php file and write code of jquery for delete and delete all function. So let's create products.blade.php file and put bellow code:
resources/views/products.blade.php
Now we are ready to run our example so run bellow command for quick run:
Hope you will understand above discussion . If you like this tutorial please leave a comments and share with your friends. If you find any error of this tutorial , please share this with me. You can read also
Also Read: How to Upload Multiple Image in Laravel
hope it can help you.
#laravel #bootstrap-confirmation-js-plugin #delete-multiple-records-via-checkbox #ajax #jquery