save multiple rows in laravel code example

Example: 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