laravel save multiple name in database code example

Example 1: multe data on database laravel

$data = $request->all();
$finalArray = array();
foreach($data as $key=>$value){
   array_push($finalArray, array(
                'fltno'=>$value['sflt'],
                'model'=>$value['smodel'],
                'engine'=>$value['sengine'],
                'loc'=>$value['sloc'],
                'serviceType'=>$value['sstye'],
                'nextSvr'=> $value['snsvr'] )
   );
});

Model::insert($finalArray);

Example 2: save multiple data in laravel

$head = Goodsreceiveheader::findorNew($request->id);
    $head->referencenumber=$request->referencenumber;
    $head->vendorid=$request->vendorid;
    $head->date=$request->date;
    $head->createdby=$request->createdby;
    if ($head->save()){
        $id = $head->id;
        foreach($request->itemid as $key =>$item_id){
            $data = array(
                            'goodsreceiveheader_id'=>$id,
                            'itemid'=>$request->itemid [$key],
                            'quantity'=>$request->quantity [$key],
                            'costprice'=>$request->costprice [$key],
                );
            Goodsreceivedetail::insert($data);
        }
    }

    Session::flash('message','You have successfully create goods receive.');

    return redirect('goodsreceive/goodsreceiveheader_list');

Tags:

Php Example