Home > front end >  how to insert multiple row data based on checkbox selection in Laravel
how to insert multiple row data based on checkbox selection in Laravel

Time:10-08

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.

blade.php file:

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

Controller 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.

CodePudding user response:

You're actually quite close, but you're accessing your Array indices wrong; $status, if defined properly, would be Issued, which is not what you want. Also, request()->all is null, but request()->all() (with the ()) would be an Array of your form input; hence the error when trying to use $data[...][...]; later.

This code should work (Note: you don't need $data at all here):

foreach(request()->input('status', []) as $index => $status) {
  DB::table('g_i_v_s')->insert([
    'item_id' => request()->input("item_id.{$index}"),
    'item_name' => request()->input("item_name.{$index}"),
    'description' => request()->input("description.{$index}"),
    'item_qty' => request()->input("item_qty.{$index}"),
    'unit_measure' => request()->input("unit_measure.{$index}"),
    'authorized_qty' => request()->input("authorized_qty.{$index}"),
    'dep_name' => request()->input("dep_name.{$index}"),
    'pr_no' => request()->input("pr_no.{$index}"),
  ]);
}

Alternative syntax would be request->input('item_id')[$index], like:

foreach(request()->input('status', []) as $index => $status) {
  DB::table('g_i_v_s')->insert([
    'item_id' => request()->input('item_id')[$index],
    'item_name' => request()->input('item_name')[$index],
    'description' => request()->input('description')[$index],
    'item_qty' => request()->input('item_qty')[$index],
    'unit_measure' => request()->input('unit_measure')[$index],
    'authorized_qty' => request()->input('authorized_qty')[$index],
    'dep_name' => request()->input('dep_name')[$index],
    'pr_no' => request()->input('pr_no')[$index],
  ]);
}

Basically, when looping over request()->input('status', []), you have access to $index, and $status (which is not referenced, but that's ok). $index will be equivalent to whatever $giv->id is on the front-end, and each field exists as an Array Index with that value, so looping and referencing is super easy.

CodePudding user response:

DD your request dd($request->all()); and then share the answer that I can understand your data array if possible.

  • Related