Ad

Update Existing Table Rows With An Array Data

- 1 answer

I am trying to update a table column with form data.

The form will pass the values in the form of an array as shown below.

enter image description here

Now I need to fill the existing table with data from the array. Something like first data into the first row.

        $infos = data::where('name', '=', 'abc')->select('*')
                ->orderBy('id')->get();

        $i = 0;
        while($i < count($infos)){
            foreach ($infos as $info) {
                $info[$i]->subject = $request->subject[$i] ? $request->subject[$i] : null;
                $info->save();
            } 
        }
Ad

Answer

After seeing the comment reply from the OP. Just use this method:

$infos = Data::where('name', 'abc')->orderBy('id')->get();

foreach($infos as $key => $info){
    $info->subject = $request->subject[$key];
    $info->save();
}
Ad
source: stackoverflow.com
Ad