'How can i save two types of dynamic input fields in laravel

I have this form Dynamic form with also some sub dynamic form

in my controller am doing

$email = $request->email;
    $info = $request->info;
    $services = $request->services;
    $service = $request->service;
    $tax = 7.5;
    $invoice = rand(0, 100000);
    $rate = $request->rate;
    $total = $request->total;

    foreach ($services as $key => $value) {
        $inv = new Invoice();
        $inv->receiver_email = $email;
        $inv->receiver_info = $info;
        $inv->render = $services[$key];
        $inv->total = $total;
        $inv->invoice = $invoice;
        $inv->tax = $tax;

        $inv->save();
        $invoice_id = [];
        // $invoice_id = $inv->id;
        $invoice_id[] = $inv->id;
        print_r($invoice_id);
        foreach ($invoice_id as $idd => $va) {
            print_r($va);
        }

            $dataforsec = [
                'service' => $service[$key],
                'rate' => $rate[$key],
                'invoice_id' => $va,
            ];
            // dd($dataforsec);
            DB::table('sections')->insert($dataforsec);

}

i think the error here is that if i create a dynamic form of two services and under each services i add like a 4 sub services under it, cause it is looping with services.. it only saves the first two of the sub services instead of saving the 4 sub services.. Please any help??

and am trying to get something here i need the sub services to be align under description by service id . see the pic below Image here



Solution 1:[1]

You can declare $invoice_id array before foreach loop

$invoice_id = [];
foreach ($services as $key => $value) {
...
...
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yeamin Hossain Raat