PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, February 4, 2022

[FIXED] update existing table rows with an array data

 February 04, 2022     laravel, model-view-controller     No comments   

Issue

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();
            } 
        }

Solution

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();
}


Answered By - Wahyu Kristianto
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing