0

I've been create a multiple input form like image below, and I want to save to database, the problem is when I using dd() it's only shown the second row data only.

How can I save each row to database?

enter image description here

here my laravel controller

public function store(Request $request)
{

        $input = Input::all();
        $condition = $input['service_id'];
        foreach ($condition as $key => $condition) {
            $detailorder = new DetailOrder;
            $detailorder->serivce_id = $input['service_id'][$key];
            $detailorder->order_type = $input['order_type'][$key];
            $detailorder->select_plan = $input['select_plan'][$key];
            $detailorder->qty = $input['qty'][$key];
            $detailorder->unit_price = $input['unit_price'][$key];
            //$detailorder->mandays = $input['mandays'][$key];
            $detailorder->note = $input['note'][$key];
            }
            dd($detailorder);

}

and here my table script, I'm using jQuery to created it

function getCorporateService(id){
    // get data and parsing to column
    $.get("{{ url('salesorder/service')}}/"+id, function(data){
        console.log(id);
        console.log(data);

        $.each(data, function (index, element){
            $br = "<tr id='item'>";
            $br += "<td> <input class='input-small' type='text' id='order_identifier' name='order_identifier[]' readonly></td>";
            $br += "<td><input class='input-small' type='text' id='service_name["+id+"]' name='service_name[]' value='"+element.service_name+"' readonly>"
                        +"<input class='input-small' type='hidden' id='service_id["+id+"]' name='service_id[]' value='"+element.id+"' readonly></td>";
            $br += "<td><select id='order_type["+id+"]' name='order_type[]'> <option> - </option> <option value='add'>Add</option> <option value='change'>Change</option> <option value='cancel'>Cancel</option> </select></td>";
            $br += "<td><input class='input-small' type='text' id='select_plan["+id+"]' name='select_plan[]'></td>";
            $br += "<td><input class='input-mini' type='text' id='qty["+id+"]' name='qty[]' value='1' onChange='getTotalPrice("+id+")'></td>";
            $br += "<td><input class='input-small' type='text' id='unit_price["+id+"]' name='unit_price[]' onChange='getTotalPrice("+id+")'></td>";
            $br += "<td><input class='input-small' type='text' id='total_price["+id+"]' name='total_price[]' onChange='getTotalPrice("+id+")'></td>";
            $br += "<td><textarea class='input-small' id='notes["+id+"]' name='note[]'></textarea></td>";
            $br += "</tr>";

            $(".corporatesvc").append($br);

        });
   });
}

4 Answers 4

2

I don't know about your dd function. Currently the loop runs and vanishes all you old orders and passing the last one in your dd function, so you need to make an array of objects and pass it in your function like,

$allOrders=array(); // make an array, for storing all detail order in it
foreach ($condition as $key => $condition) {
    $detailorder = new DetailOrder;

    //you can use to ignore $key::=> $detailorder->serivce_id = $condition['service_id'];

    $detailorder->serivce_id = $input['service_id'][$key];
    $detailorder->order_type = $input['order_type'][$key];
    $detailorder->select_plan = $input['select_plan'][$key];
    $detailorder->qty = $input['qty'][$key];
    $detailorder->unit_price = $input['unit_price'][$key];
    //$detailorder->mandays = $input['mandays'][$key];
    $detailorder->note = $input['note'][$key];
    $allOrders[]=$detailorder;
}
dd($allOrders); // pass it to the function
Sign up to request clarification or add additional context in comments.

Comments

2

In the public function store(Request $request) add the line as shown below

public function store(Request $request)
{

        $input = Input::all();
        $condition = $input['service_id'];
        foreach ($condition as $key => $condition) {
            $detailorder = new DetailOrder;
            $detailorder->serivce_id = $input['service_id'][$key];
            $detailorder->order_type = $input['order_type'][$key];
            $detailorder->select_plan = $input['select_plan'][$key];
            $detailorder->qty = $input['qty'][$key];
            $detailorder->unit_price = $input['unit_price'][$key];
            //$detailorder->mandays = $input['mandays'][$key];
            $detailorder->note = $input['note'][$key];

            //for saving each DetailOrder to database
            $detailorder->save();
            } 
}

Notice here that the save() should be invoked from within the foreach loop for saving each row to database.

Considering that your JavaScript is working properly , the above modification will save data of each row in your database.

Comments

1

Check the following line:

foreach ($condition as $key => $condition) {
    $detailorder = new DetailOrder;
    // here you are creating a new object on each iteration, means on each iteration new object overrides old one, that's why you are getting the last record only
}

To solve this issue put this object creation code outside the loop and try again.

Comments

1

You can insert multi items with this code:

DB::table('tablename')->insert($tableitems);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.