PHP Laravel 6 SweetAlert jQuery Ajax Delete Rows Example

Today, We want to share with you PHP Laravel 6 SweetAlert jQuery Ajax Delete Rows Example.In this post we will show you Laravel 6 jQuery Ajax CRUD Example with Sweet Alert, hear for How to use sweet alert for delete confirm in Laravel 6? we will give you demo and example for implement.In this post, we will learn about Ajax SweetAlert for Live Data Deleting Rows in with PHP Laravel 6 with MySQL and jQuery with an example.

PHP Laravel 6 SweetAlert jQuery Ajax Delete Rows Example

There are the Following The simple About Laravel 6 Sweet Alert AJAX CRUD Tutorial Full Information With Example and source code.

As I will cover this Post with live Working example to develop Laravel 6 confirmation box for delete a member from database example, so the Display Sweet Alert in Laravel 6 using uxweb/sweet-alert is used for this example is following below.

Phase 1: Download laravel

composer create-project --prefer-dist laravel/laravel aitsMGM

Phase 2: Create Database Migration

php artisan make:model School -m

Create a School model and schools table

bigIncrements('id');
            $table->string('name');
            $table->string('locations');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('schools');
    }
}

app/School.php


Phase 3: Define routes

routes/web.php

Route::get('/school', 'SchoolController@view')->name('school.index');
Route::get('/schools', 'SchoolController@get_school_data')->name('data');
Route::get('/addschool', 'SchoolController@view')->name('school.view');
Route::post('/addschool', 'SchoolController@Store')->name('school.store');
Route::delete('/addschool/{id}', 'SchoolController@destroy')->name('school.destroy');
Route::get('/addschool/{id}/edit', 'SchoolController@update')->name('school.update');

Phase 4: Create a Laravel 6 Controller

php artisan make:controller SchoolController

app/Http/Controllers/SchoolController.php

paginate(5);

      return Request::ajax() ? 
                   response()->json($schools,Response::HTTP_OK) 
                   : abort(404);
  }

  public function Store(Request $request)
  {
    School::updateOrCreate(
      [
        'id' => $request->id
      ],
      [
        'name' => $request->name,
        'locations' => $request->locations
      ]
    );

    return response()->json(
      [
        'success' => true,
        'message' => 'Data inserted successfully'
      ]
    );
  }

  public function update($id)
  {
    $comapny  = School::find($id);

    return response()->json([
      'data' => $comapny
    ]);
  }
  
  public function destroy($id)
  {
    $school = School::find($id);

    $school->delete();

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

}

Phase 5: Create View File

resources/views/layouts/app.blade.php





    
    
    

    PHP Laravel 6 ajax crud - infinityknow 

    
    
    




        
@yield('content')
@stack('ajax_crud')

resources/views/school/index.blade.php

@extends('layouts.app')

@section('content')

Add School
Name Address Action
@include('school.modal')
@endsection @push('ajax_crud') @endpush

resources/views/school/modal.blade.php

 

public/js/ajax.js

$(document).ready(function () {
get_school_data()

$.ajaxSetup({
    headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
});

//Get all school
function get_school_data() {
    
	$.ajax({
        url: root_url,
        type:'GET',
    	data: { }
	}).done(function(data){
        table_data_row(data.data)
	});
}

//School table row
function table_data_row(data) {

    var	rows = '';
    
	$.each( data, function( key, value ) {
        
	  	rows = rows + '';
	  	rows = rows + ''+value.name+'';
	  	rows = rows + ''+value.locations+'';
	  	rows = rows + '';
                rows = rows + 'Edit ';
                rows = rows + 'Delete ';
                rows = rows + '';
	  	rows = rows + '';
	});

	$("tbody").html(rows);
}

//Insert school data
$("body").on("click","#createNewSchool",function(e){

    e.preventDefault;
    $('#studentCrudModal').html("Create school");
    $('#submit').val("Create school");
    $('#student-id').modal('show');
    $('#school_id').val('');
    $('#schooldata').trigger("reset");

});

//Save data into database
$('body').on('click', '#submit', function (event) {
    event.preventDefault()
    var id = $("#school_id").val();
    var name = $("#name").val();
    var locations = $("#locations").val();
   
    $.ajax({
      url: store,
      type: "POST",
      data: {
        id: id,
        name: name,
        locations: locations
      },
      dataType: 'json',
      success: function (data) {
          
          $('#schooldata').trigger("reset");
          $('#student-id').modal('hide');
          Swal.fire({
            position: 'top-end',
            icon: 'success',
            title: 'Success',
            showConfirmButton: false,
            timer: 1500
          })
          get_school_data()
      },
      error: function (data) {
          console.log('Error......');
      }
  });
});

//Edit modal window
$('body').on('click', '#editSchool', function (event) {

    event.preventDefault();
    var id = $(this).data('id');
   
    $.get(store+'/'+ id+'/edit', function (data) {
         
         $('#studentCrudModal').html("Edit school");
         $('#submit').val("Edit school");
         $('#student-id').modal('show');
         $('#school_id').val(data.data.id);
         $('#name').val(data.data.name);
         $('#locations').val(data.data.locations);
     })
});

 //DeleteSchool
 $('body').on('click', '#deleteSchool', function (event) {
    if(!confirm("Do you really want to do this?")) {
       return false;
     }

     event.preventDefault();
    var id = $(this).attr('data-id');
 
    $.ajax(
        {
          url: store+'/'+id,
          type: 'DELETE',
          data: {
                id: id
        },
        success: function (response){
          
            Swal.fire(
              'Remind!',
              'School deleted successfully!',
              'success'
            )
            get_school_data()
        }
     });
      return false;
   });

}); 
Web Programming Tutorials Example with Demo

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about sweet alert php response.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment