Skip to content
Advertisement

Save all record records using request->all in Laravel

Laravel provides a great help for developers to save all input fields of a form which is one record with one line of code.

like if I want to save a form which has multiple input fields and one record to database like:

enter image description here

then I can save it with below code and it works great:

SaveOrder:: create($request->all());

Now I have a question. If I have multiple records (multiple rows) in a form and I can add new rows with a button pressed. Then how can I save all records with above code?

Like: enter image description here

Advertisement

Answer

The best answer for this question is using foreach statement. Like:

    $CustomerName= $request -> input('CustomerName');
    $ProductId= $request -> input('ProductId');
    $ProductName= $request -> input('ProductName');
    $ProductColor= $request -> input('ProductColor');

    foreach( $ProductId as $key => $n ) {
        SaveOrder::insert(
                    array(
                        'CustomerName' => $CustomerName[$key],
                        'ProductId' => $ProductId[$key],
                        'ProductName' => $ProductPrice[$key],
                        'ProductColor' => $ProductQuantity[$key],
                    )
                    );}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement