mardi 12 septembre 2017

table checkbox in laravel delete multiple data with foreign key error

i have a checkbox list of products and i want to delete all the data and also from its foreign key in other table using checkbox.

Here is what i've got, from my table i created a form that is being routed to delete data that is checked from the checkbox.

<form action="" method="get" id="delprod">
    

    <table class="table table-hover">                                    
        <thead>                        
            <tr>
              <th width="4%"></th>
              <th width="17%">SUB-CATEGORY</th>
              <th width="30%">PRODUCT NAME</th>
              <th width="10%">QTY</th>
              <th width="10%">STATUS</th>
              <th width="25%">UPDATED</th>                
            </tr>
        </thead>
        <tbody>
             @foreach($products as $key => $data)
            <tr>
                <td><input type="checkbox" name="products[]" value="" />&nbsp;</td>                            
                <td><a href="#">  </a></td>
                <td><a href="#">  </a></td>
                <td><a href="#">  </a></td>
                <td><a href="#">  </a></td>
                <td></td>                            
            </tr>
            @endforeach
        </tbody>
    </table>
    </form>
<p data-placement="top" data-toggle="tooltip" title="Delete"><button class="btn btn-danger btn-sm pull-right delbtn" data-title="Delete" data-toggle="modal" data-target="#delete" ><span class="glyphicon glyphicon-trash"></span></button></p>

ive made my button outside the form but i have created a javascript for it.

$(".delbtn").click( function() {
    $('#delprod').submit();
});

in my controller here is what i did:

public function deleteProducts(Request $request) {

    Product::destroy($request->products);

    return redirect()->route('admin.product');
}

i got an error because this Product table has a foreign key from table Product_image how can i also make a controller to delete all data in my Product_image table that has product_id that is being checked in checkbox?

i tried to make a model Product_image and tried this in my controller

Product_image::destroy($request->products); 

but it gives me error because it says it does't exist. Any suggestions in making a query to delete a foreign key that is being passed by checkbox? . something like deleting a query that accepts array from checkbox.?




Aucun commentaire:

Enregistrer un commentaire