vendredi 7 octobre 2022

how to insert multiple row data based on checkbox selection in Laravel

hope you all are doing well. I'm stuck with multiple row insertion in laravel. I keep on getting an error which says: Trying to access array offset on value of type null. My code is below as follows;

blade.php file:

<tbody>
                             
                            @forelse($GIV as $GIV)
                                <tr>
                                    <td> <input type="checkbox" name="status[]" id="status" value="Issued" ></td>
                                    <td> <input type="text"   class="form-control"   id="id"   name="item_id[]"  hidden="" value=""></td>
                                    <td> <input type="text"   class="form-control"   id="pr_no"          name="pr_no[]"          hidden="" value=""></td>
                                    <td> <input type="text"   class="form-control"   id="dep_name"       name="dep_name[]"       hidden="" value=""></td>
                                    <td> <input type="hidden"   class="form-control"   id="item_name"      name="item_name[]"    value=""></td>
                                    <td> <input type="text"   class="form-control"   id="description"    name="description[]"    hidden="" value="" ></td>
                                    <td> <input type="number" class="form-control"   id="item_qty"       name="item_qty[]"       hidden="" value=""></td>
                                    <td> <input type="text"   class="form-control"   id="unit_measure"   name="unit_measure[]"   hidden="" value=""></td>
                                    <td> <input type="number" class="form-control"   id="authorized_qty" name="authorized_qty[]" hidden="" value=""></td>
                                    
                                </tr>
                             
                                @empty
                                <tr><td colspan="16"><center>No Data Found</center</td></tr>
                                @endforelse </tbody>                                                                    

Control file:

public function addDataGIV(Request $request)
{
   $data =request()->all;
    foreach(request()->status as $status)
    {
        DB::table('g_i_v_s')
        ->insert(
            [
                
                'item_id'       =>$data['item_id'][$status],
                'item_name'     =>$data['item_name'][$status],
                'description'   =>$data['description'][$status],
                'item_qty'      =>$data['item_qty'][$status],
                'unit_measure'  =>$data['unit_measure'][$status],
                'authorized_qty'=>$data['authorized_qty'][$status],
                'dep_name'      =>$data['dep_name'][$status],
                'pr_no'         =>$data['pr_no'][$status],
                ]
                );
     }
   }

When a user selects the checkbox and clicks the submit button, I want only the selected checkbox row including the checkbox value to be stored into the database, without any errors. I kindly ask for help.

Thanks, in advance :)




Aucun commentaire:

Enregistrer un commentaire