laravel update multiple records code example

Example 1: update many laravel

$post->comments()->updateMany([
    [
        'message' => 'A new comment.',
    ],
    [
        'message' => 'Another new comment.',
    ],
]);

Example 2: laravel updateorcreate multiple records

// If there's a flight from Oakland to San Diego, set the price to $99.
// If no matching model exists, create one.
$flight = App\Flight::updateOrCreate(
    ['departure' => 'Oakland', 'destination' => 'San Diego'],
    ['price' => 99]
);

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