Search code examples
checkboxlaravel-5.4sql-delete

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="{{ route('admin.delproducts') }}" method="get" id="delprod">
    {{ csrf_field() }}

    <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="{{ $data->product_id }}" />&nbsp;</td>                            
                <td><a href="#"> {{ $data->name }} </a></td>
                <td><a href="#"> {{ $data->product_name }} </a></td>
                <td><a href="#"> {{ $data->quantity }} </a></td>
                <td><a href="#"> {{ $data->status }} </a></td>
                <td>{{ $data->updated_at }}</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.?


Solution

  • If you have a relation between Product Image you can do this. (I assume your relation name is productImage. Laravel destroy function does not removes relationships. If you didnt define onDelete('cascade')

    If you want to delete relations you need to define onDelete on your migration file or in database directly.

      $table
        ->foreign('product_id')
        ->references('id')
        ->on('product_image')
        ->onDelete('cascade');
    

    After that you can try using destroy (not sure if it removes relationships) if its not then you can do this

    public function deleteProducts(Request $request) {
        foreach ($request->products as $product_id) {
            $product = Product::find($id);
            $product->productImage()->delete();
        }
    
        return redirect()->route('admin.product');
    }
    

    If no relation than you need to remove relational data manually.