table checkbox in laravel delete multiple data with foreign key error -


i have checkbox list of products , want delete data , foreign key in other table using checkbox.

here i've got, table created form being routed delete data checked 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 $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 button outside form have created javascript it.

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

in controller here did:

public function deleteproducts(request $request) {      product::destroy($request->products);      return redirect()->route('admin.product'); } 

i got error because product table has foreign key table product_image how can make controller delete data in product_image table has product_id being checked in checkbox?

i tried make model product_image , tried in controller

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

but gives me error because says does't exist. suggestions in making query delete foreign key being passed checkbox? . deleting query accepts array checkbox.?

if have relation between product image can this. (i assume relation name productimage. laravel destroy function not removes relationships. if didnt define ondelete('cascade')

if want delete relations need define ondelete on migration file or in database directly.

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

after can try using destroy (not sure if removes relationships) if not can

public function deleteproducts(request $request) {     foreach ($request->products $product_id) {         $product = product::find($id);         $product->productimage()->delete();     }      return redirect()->route('admin.product'); } 

if no relation need remove relational data manually.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -