Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

achieve100's avatar

how to save a multiple rows of records from one database table to another table at the same database in laravel 5.4?

<!-- I am trying to fetch data from one table to the web table, then, add some input data and save it to the second table in the same database in Laravel 5.4. At this moment, 
  
  --<1. the system can only realize the date field, all other fields are null. Can't really copy the data from one table to another table, the selection field can't be detected as a value but "null". -->
  
  <!-- 2. Even the first item is successful, my code is only able to save one row record not multiple records. Do I need somehow to set ID for each row of records before I can save. But, I don't know how.  -->
  
  <!-- Thanks for help.  --> 

     
0 likes
2 replies
jlrdw's avatar

Not your data, but here I am purging cleared ckecks, they are backed up to another table, a csv file is generated of cleared checks. Not laravel, but may give some ideas.

public function checkPurge()
    {
        $stmt = $this->db->select("SELECT * FROM checks ORDER BY checkid", array(), \PDO::FETCH_ASSOC);
        $k = 0;
        $numbers[0] = 0;
        $fileout = fopen("cleared.txt", "wb");
        foreach ($stmt as $row) {

            $k = $k + 1;
            if ($k < 2) {
                $checkid = $row['checkid'];
                $transdate = $row['transdate'];
                $transdrscribe = $row['transdescribe'];
                $wd = Cln::fixInt($row['widthdraw']);
                $dep = Cln::fixInt($row['deposit']);
                $isclr = Cln::fixTiny($row['isclr']);
            } else {
                if ($row['isclr'] == 1) {
                    $checkid = $row['checkid'];
                    $transdate = $row['transdate'];
                    $transdrscribe = $row['transdescribe'];
                    $wd = Cln::fixInt($row['widthdraw']);
                    $dep = Cln::fixInt($row['deposit']);
                    $isclr = Cln::fixTiny($row['isclr']);
                } else {
                    $k = 100000;
                }
            }

            $newline = $checkid . ',' . $transdate . ',' . $transdrscribe . ',' . $wd . ',' . $dep . ',' . $isclr . PHP_EOL;

            $postdata = array(
                'checkid' => $checkid,
                'transdate' => $transdate,
                'transdescribe' => $transdrscribe,
                'widthdraw' => $wd,
                'deposit' => $dep,
                'isclr' => $isclr,
                'PRTED' => 'y'
            );


            if ($k > 99999)
                break;


            fputcsv($fileout, explode(',', $newline));
            $this->db->insert("checkbacks", $postdata);  //HERE `HERE`
            $numbers[$k] = $row['checkid'];
        }
        fclose($fileout);
        $chktokeep = end($numbers);
        reset($numbers);

        $postdata = array(
            'transdescribe' => "Start Amt",
            'widthdraw' => NULL,
            'deposit' => $rs,
            'isclr' => 1,
        );
        $where = array('checkid' => $chktokeep);
        $this->db->update("checks", $postdata, $where);

        foreach ($numbers as $value) {
            if ($value < $chktokeep && $value != 0) {
                $this->db->delete("checks", array('checkid' => $value));
            }
        }

        return;
    }

Basically cleared checks are inserted into the checkbacks table. isclr is a checkbox.

Please or to participate in this conversation.