How to save it in database

127 views Asked by At

In my database i have column like id,product_id,company_name,service,qty,delivery_cost,delivery_date,order_status etc.

I view i used Jquery and Html and jquery dynamically add more input fields of product_id,service,delivery_cost,qty,delivery_date,order_status on clicking ADD more button.On submiting form i got this in controller on doing dd($allData);

My question is how can i save this data in database

array:8 [▼
  "_token" => "gSddIeA11OBV60xU9YiDXn8fmsfkwxlQ85QmDdkQ"

  "service" => array:3 [▼
    0 => "cement"
    1 => "iron"
    2 => "steel"
  ]
  "qty" => array:3 [▼
    0 => "5"
    1 => "44"
    2 => "5"
  ]
  "delivery_cost" => array:3 [▼
    0 => "5465"
    1 => "553"
    2 => "554"
  ]
  "delivery_date" => array:3 [▼
    0 => "2016-12-16"
    1 => "2016-12-08"
    2 => "2016-12-17"
  ]
  "order_status" => "Confirm"
  "delivery_vehicle" => array:1 [▼
    0 => "Self_elivery"
   1 => "Self_elivery"
   2 => "Self_elivery"
  ]
]
public function store(Request $request)
    {
        $allData= $request->all();
        dd($allData);
         $product = new Order;


   }

i try this

public function store(Request $request)
    {


        $date = $request->get('delivery_date');
        $cost = $request->get('delivery_cost');
        $service = $request->get('service');//add quotes next to service

        foreach($date as $deliveryDate)
        {
           foreach($cost as $proAmount){
            $db = new Order;
            $db->delivery_date = $deliveryDate;
            $db->amount = $proAmount;
            $db->save();

        }
}
        return"ok";
} 

I tried this way but it store same data multiple times may be because of loop inside of loop.I need your help to store this data in database

4

There are 4 answers

0
Md.Jewel Mia On
Using foreach() should work for you very easy:

$inputdata = $request->all();

foreach ($inputdata as $key=>$val) {

    $dbdata = new Order;
    $dbdata ->delivery_date = $data['delivery_date'][$key];
    $dbdata ->delivery_cost = $data['delivery_cost'][$key];
    ....   
    $dbdata ->save();
}
3
Alexey Mezenin On

Using for() should work for you:

$data = $request->all();

for ($i = 0; $i < count($data['delivery_date']); $i++) {
    $db = new Order;
    $db->delivery_date = $data['delivery_date'][$i];
    $db->delivery_cost = $data['delivery_cost'][$i];
    ....
    $db->save();
}
0
Rohit shah On

You can try this If you want to use foreach $key will give you the index.

        $date = $request->get('delivery_date');
        $cost = $request->get('delivery_cost');
        $service = $request->get('service');

        foreach($date as $key=>$deliveryDate)
        {
            $db = new Order;
            $db->delivery_date = $deliveryDate;
            $db->amount = $cost[$key];
            $db->save();
        }

        return"ok";

Hope this help you. Ask if any query

1
Rajender Joshi On

Do bulk insert instead of running new sql query for every insert(if all the request params exist in single table).

  $data = [];

  foreach ($request->all() as $param => $val) {
    if( is_array($val) ) // Ignore string params. i.e. _token, order_status
    {
      foreach ($val as $key => $value) {

        $data[$index][$param] = $value;
        $data[$index]['created_at'] = \Carbon\Carbon::now();
        $data[$index]['updated_at'] = \Carbon\Carbon::now();

        $index++;
      }
      $index = 0;
    }
  }

  Model::insert($data);