B
B
BonBon Slick2016-11-08 15:44:51
JavaScript
BonBon Slick, 2016-11-08 15:44:51

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails... Laravel 5.3?

Ajax:

$(document).on('click', '#mass_delete', function( ) {
         var values = [];
         var token = $(this).data('token');
         $('input[type="checkbox"]:checked').each(function() {
           if( $.isNumeric( $(this).val() )  ){
             values.push( $(this).val() );
           }
         });
         // alert(values);
         $.ajax({
          url: 'products/mass_delete',
           type: 'delete',
           data: {
             "delete_ids": values,
             "_method": 'DELETE',
             "_token": token,
           },
         })
         .done(function( response ) {
           console.log("Success! Rows deleted.");
         })
         .fail(function() {
           console.log("ERROR!!!");
         })
         $('input[type="checkbox"]:checked').each(function() {
           $(this).parents('tr').remove();
         });
       });

route
Route::delete( 'products/mass_delete', '[email protected]_delete' );

Controller
public function mass_delete(Request $request){
  $product_ids = $request->get('delete_ids');
  if ( is_array( $product_ids ) && !empty( $product_ids ) ) {
    Product::destroy( $product_ids );
  }
}

It seems to me that it does not delete because there are comments on the product, then all the comments on the product must be deleted.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question