Last active
January 5, 2018 20:37
-
-
Save talha08/2ba3952a4466f202aa0b0a0c94ff718f to your computer and use it in GitHub Desktop.
Laravel AJax Delete http://itsolutionstuff.com/post/laravel-5-confirmation-before-delete-record-from-database-exampleexample.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| public function destroy($id, Request $request) | |
| { | |
| try{ | |
| $product = ProductSale::findOrFail( $id ); | |
| if ( $request->ajax() ) { | |
| $product->delete( $request->all() ); | |
| return response(['msg' => 'Product deleted', 'status' => 'success']); | |
| } | |
| }catch (\Exception $x){ | |
| return response(['msg' => 'Failed deleting the product', 'status' => 'failed']); | |
| } | |
| } | |
| ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-confirmation/1.0.5/bootstrap-confirmation.min.js"></script> | |
| jQuery(document).ready(function() { | |
| $('[data-toggle=confirmation]').confirmation({ | |
| rootSelector: '[data-toggle=confirmation]', | |
| onConfirm: function (event, element) { | |
| element.trigger('confirm'); | |
| } | |
| }); | |
| $('.deleteProduct').on('confirm', function(e) { | |
| var ele = e.target; | |
| var dataId = $('#btnDeleteProduct').attr('data-id'); | |
| var parent = $(this).parent(); | |
| $.ajax({ | |
| url: ele.href, | |
| type: 'DELETE', | |
| headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}, | |
| success: function( msg ) { | |
| if ( msg.status === 'success' ) { | |
| toastr.success( msg.msg ); | |
| //don't need to reload your page, just remove the row from DOM | |
| parent.slideUp(300, function () { | |
| parent.closest("tr").remove(); | |
| }); | |
| // setInterval(function() { | |
| // window.location.reload(); | |
| // }, 5900); | |
| } | |
| }, | |
| error: function( data ) { | |
| if ( data.status === 422 ) { | |
| toastr.error('Cannot delete the product'); | |
| } | |
| } | |
| }); | |
| return false; | |
| }); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Route::delete('productSale/{id}',['as' => 'productSale.delete', 'uses' => 'ProductSaleController@destroy']); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <meta name="csrf-token" content="{{ csrf_token() }}"> | |
| <td> | |
| <a href="{{ route('productSale.delete',$productSale->id) }}" class="btn btn-danger btn-sm deleteProduct" | |
| data-id="{{$productSale->id}}" | |
| id="btnDeleteProduct" | |
| 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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment