How to install laravel 9
https://tutorial101.blogspot.com/2022/02/how-to-install-laravel-9.html
Connecting our Database
open .env file root directory.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
Create tables Model
php artisan make:model Product -m
C:\xampp\htdocs\laravel\blog>php artisan make:model Product -m
Open new products migrations
yourproject/database/migrations
blog\database\migrations\_create_products_table.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | //blog\database\migrations\_create_products_table.php <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create( 'products' , function (Blueprint $table ) { $table ->id(); $table ->string( 'prodname' ); $table ->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists( 'products' ); } }; |
php artisan migrate
C:\xampp\htdocs\laravel9\blog>php artisan migrate
Migration table created successfully.
check database table
Creating Controller
php artisan make:controller ProductController
C:\xampp\htdocs\laravel\blog>php artisan make:controller ProductController
change it with the following codes:
blog\app\Http\Controllers\ProductController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | //blog\app\Http\Controllers\ProductController.php <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use DB; class ProductController extends Controller { /** * Show the application dashboard. * * @return \Illuminate\Http\Response */ public function index() { $products = DB::table( "products" )->get(); return view( 'index' ,compact( 'products' )); } /** * Show the application dashboard. * * @return \Illuminate\Http\Response */ public function destroy( $id ) { DB::table( "products" )-> delete ( $id ); return response()->json([ 'success' => "Product Deleted successfully." , 'tr' => 'tr_' . $id ]); } /** * Show the application dashboard. * * @return \Illuminate\Http\Response */ public function deleteAll(Request $request ) { $ids = $request ->ids; DB::table( "products" )->whereIn( 'id' , explode ( "," , $ids ))-> delete (); return response()->json([ 'success' => "Products Deleted successfully." ]); } } |
blog\resources\views\index.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | //blog\resources\views\index.blade.php <!DOCTYPE html> <html> <head> <title>Laravel 9 Delete Multiple Records with checkbox using Jquery Ajax</title> <link rel= "stylesheet" href= "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" > <script src= "https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js" ></script> <script src= "https://cdnjs.cloudflare.com/ajax/libs/bootstrap-confirmation/1.0.5/bootstrap-confirmation.min.js" ></script> <meta name= "csrf-token" content= "{{ csrf_token() }}" > </head> <body> <div class = "container" > <h3>Laravel 9 Delete Multiple Records with checkbox using Jquery Ajax</h3> <button style= "margin-bottom: 10px" class = "btn btn-primary delete_all" data-url= "{{ url('myproductsDeleteAll') }}" > Delete All Selected</button> <table class = "table table-bordered table-striped" > <thead> <th width= "50px" ><input type= "checkbox" id= "master" ></th> <th width= "80px" >No</th> <th>Product Name</th> <th width= "100px" >Action</th> </thead> <tbody> @ if ( $products -> count ()) @ foreach ( $products as $key => $product ) <tr id= "tr_{{$product->id}}" > <td><input type= "checkbox" class = "sub_chk" data-id= "{{$product->id}}" ></td> <td>{{ ++ $key }}</td> <td>{{ $product ->prodname }}</td> <td> <a href= "{{ url('myproducts',$product->id) }}" class = "btn btn-danger btn-sm" data-tr= "tr_{{$product->id}}" data-toggle= "confirmation" data-btn-ok-label= "Delete" data-btn-ok-icon= "fa fa-remove" data-btn-ok- class = "btn btn-sm btn-danger" data-btn-cancel-label= "Cancel" data-btn-cancel-icon= "fa fa-chevron-circle-left" data-btn-cancel- class = "btn btn-sm btn-default" data-title= "Are you sure you want to delete ?" data-placement= "left" data-singleton= "true" > Delete </a> </td> </tr> @ endforeach @ endif </tbody> </table> </div> <script type= "text/javascript" > $(document).ready( function () { $( '#master' ).on( 'click' , function (e) { if ($(this).is( ':checked' ,true)) { $( ".sub_chk" ).prop( 'checked' , true); } else { $( ".sub_chk" ).prop( 'checked' ,false); } }); $( '.delete_all' ).on( 'click' , function (e) { var allVals = []; $( ".sub_chk:checked" ).each( function () { allVals.push($(this).attr( 'data-id' )); }); if (allVals.length <=0) { alert( "Please select row." ); } else { var check = confirm( "Are you sure you want to delete this row?" ); if (check == true){ var join_selected_values = allVals.join( "," ); $.ajax({ url: $(this).data( 'url' ), type: 'DELETE' , headers: { 'X-CSRF-TOKEN' : $( 'meta[name="csrf-token"]' ).attr( 'content' )}, data: 'ids=' +join_selected_values, success: function (data) { if (data[ 'success' ]) { $( ".sub_chk:checked" ).each( function () { $(this).parents( "tr" ).remove(); }); alert(data[ 'success' ]); } else if (data[ 'error' ]) { alert(data[ 'error' ]); } else { alert( 'Whoops Something went wrong!!' ); } }, error: function (data) { alert(data.responseText); } }); $.each(allVals, 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(); $.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" ); alert(data[ 'success' ]); } else if (data[ 'error' ]) { alert(data[ 'error' ]); } else { alert( 'Whoops Something went wrong!!' ); } }, error: function (data) { alert(data.responseText); } }); return false; }); }); </script> </body> </html> |
blog\routes\web.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | //blog\routes\web.php <?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\ProductController; //add ProductController /* |-------------------------------------------------------------------------- | Web Routes |-------------------------------------------------------------------------- | | Here is where you can register web routes for your application. These | routes are loaded by the RouteServiceProvider within a group which | contains the "web" middleware group. Now create something great! | */ Route::get( '/' , function () { return view( 'welcome' ); }); Route::get( '/myproducts' , [ProductController:: class , 'index' ]); Route:: delete ( '/myproducts/{id}' , [ProductController:: class , 'destroy' ]); Route:: delete ( '/myproductsDeleteAll' , [ProductController:: class , 'deleteAll' ]); |
Starting Laravel development server: http://127.0.0.1:8000