how to delete Multiple Selected rows using checkbox in laravel

how to delete Multiple Selected rows using checkbox in laravel

In this Post We Will Explain About is how to delete Multiple Selected rows using checkbox in laravel With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to Laravel Delete multiple rows from mysql with checkbox Example

In this post we will show you Best way to implement Delete multiple records from MySQL in Laravel, hear for with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

how to delete multiple records using checkboxes in Laravel

Phase 1: Create items Table with fake Records

fake Records Query:Here, We Simple have to create all the “items” simple sql table then We can run mysql simple sql query for fake records. We can make items table using simple database like as a migration After that also make some fake records using like as a seeder. Therefor now We just simple run sql query.

INSERT INTO `items` (`id`, `name`, `details`, `created_at`, `updated_at`) VALUES
(1, 'Laptop', 'Laptop posts', NULL, NULL),
(3, 'Mobile', 'Mobile posts', NULL, NULL),
(4, 'DVD', 'DVD posts', NULL, NULL),
(5, 'Computer', 'Computer posts', NULL, NULL),
(6, 'Tablet', 'Tablet posts', NULL, NULL);

Phase 2: Create new Routes

Route::get('myitems', 'ItemController@index');
Route::delete('myitems/{id}', 'ItemController@destroy');
Route::delete('myitemsDeleteAll', 'ItemController@deleteAll');

Phase 3: Add ItemController

app/Http/Controllers/ItemController.phpPlease put this source code in this path

get();
        return view('items',compact('items'));
    }

    public function destroy($id)
    {
    	DB::table("items")->delete($id);
    	return response()->json(['success'=>"Item Deleted successfully.", 'tr'=>'tr_'.$id]);
    }

    public function deleteAll(Request $request)
    {
        $item_ids = $request->item_ids;
        DB::table("items")->whereIn('id',explode(",",$item_ids))->delete();
        return response()->json(['success'=>"Items Deleted successfully."]);
    }
}

Phase 4: Add Blade File

resources/views/items.blade.php Please put this source code in this path




    Laravel 5 step by step - Multiple some delete records using checkbox example
    
    
    
    
    




 




    $(document).ready(function () {

        $('#live_master').on('click', function(e) {
         if($(this).is(':checked',true))  
         {
            $(".livesub_chk").prop('checked', true);  
         } else {  
            $(".livesub_chk").prop('checked',false);  
         }  
        });

        $('.all_delete').on('click', function(e) {

            var dataValues = [];  
            $(".livesub_chk:checked").each(function() {  
                dataValues.push($(this).attr('data-id'));
            });  

            if(dataValues.length <=0)  
            {  
                console.log("Please some records select any row.");  
            }  else {  

                var check = confirm("Please Confirm Are We sure We want to delete this row?");  
                if(check == true){  

                    var join_selected_values = dataValues.join(","); 

                    $.ajax({
                        url: $(this).data('url'),
                        type: 'DELETE',
                        headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                        data: 'item_ids='+join_selected_values,
                        success: function (data) {
                            if (data['success']) {
                                $(".livesub_chk:checked").each(function() {  
                                    $(this).parents("tr").remove();
                                });
                                console.log(data['success']);
                            } else if (data['error']) {
                                console.log(data['error']);
                            } else {
                                console.log('Sorry Something went wrong!!');
                            }
                        },
                        error: function (data) {
                            console.log(data.responseText);
                        }
                    });

                  $.each(dataValues, function( index, value ) {
                      $('table tr').filter("[data-row-id='" + value + "']").remove();
                  });
                }  
            }  
        });

        $('[data-toggle=confirmation]').confirmation({
            rootSelector: '[data-toggle=confirmation]',
            onConfirm: function (event, element) {
                element.trigger('confirm');
            }
        });

        $(document).on('confirm', function (e) {
            var ele = e.target;
            e.preventDefault();
			//call ajax
            $.ajax({
                url: ele.href,
                type: 'DELETE',
                headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
                success: function (data) {
                    if (data['success']) {
                        $("#" + data['tr']).slideUp("slow");
                        console.log(data['success']);
                    } else if (data['error']) {
                        console.log(data['error']);
                    } else {
                        console.log('Sorry Something went wrong!!');
                    }
                },
                error: function (data) {
                    console.log(data.responseText);
                }
            });

            return false;
        });
    });



php artisan serve

And then last setp to you can open simple bellow url on your any browser:

http://localhost:8000/myitems

You are Most welcome in my youtube Channel Please shubscibe my channel. and give me FeedBackMore Details……
Angularjs Example

Example

I hope you have Got What is how to delete multiple rows using checkbox in php with Laravel And how it works.I would Like to have FeedBack From My Blog(Pakainfo.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(Pakainfo.com) Are Most Always Welcome.

Leave a Comment