leviathanz's avatar

Updating db data value using csv import

Hi All,

I created a controller to update the existing data using csv import in laravel controller within initial step (to download the existing data value from db) and finalize upload file expected to handle to update data in my DB. The csv file column is: id, name, code, type, cost, region, shipping, tax

here the controller so far:

    public function updateDif($id)
    {
        $update_product_data = UpdateProduct::find($id);
        $file_handle = fopen('public/update_product/'.$update_product_data->final_file, 'r');
		$data = [];
		$update_product_data = Product::updateOrCreate(
			['id' => $data[1]],
			[
				"name" => $data[2],
				"code" => $data[3],
				"type" => $data[4],
				"cost" => $data[5],
				"region" => $data[6],
				"shipping" => $data[7],
				"tax" => $data[8]
			]
		);
        return $data;
    }

my code not working for update the data, is only uploaded a csv file :(.

Any suggestion how to achieve this?

Thanks,

0 likes
5 replies
jlrdw's avatar
jlrdw
Best Answer
Level 75

Is there only one row in your csv file?

I do a csv import in another framework, it's

    public function incsv() {
        $file = 'C:\mydocs\csv.csv';
        $content = file($file);
        $array = array();

        for ($i = 1; $i < count($content); $i++) {
            $line = explode(',', $content[$i]);
            for ($j = 0; $j < count($line); $j++) {
                $array[$i][$j + 1] = $line[$j];
            }
        }

        $k = count($array) + 1;

        for ($i = 1; $i < $k; $i++) {
            $tdate = new \DateTime($array[$i][2]);
            $ndate = $tdate->format('Y-m-d');
            $descraw = $array[$i][8];
            $descspace = preg_replace('/[^a-z\d ]/i', '', $descraw);
            $desc = preg_replace('/^\s+|\s+$|\s+(?=\s)/', '', $descspace);
            $amt = (float) $array[$i][3];
            $namt = number_format($amt, 2, '.', '');
            if ($namt < 0) {
                $wd = $namt * -1;
                $dep = 0;
            } else {
                $wd = 0;
                $dep = $namt;
            }

            $maxid = $this->Check->getMaxid();
            $checkid = $maxid + 1;

            $data = [
                'checkid' => $checkid,
                'transdate' => $ndate,
                'transdescribe' => $desc,
                'widthdraw' => $wd,
                'deposit' => $dep
            ];
            $this->Check->insertCsv($data);
        }

        $this->Check->checkRecalc();
        Url::redirect('check/index');
    }

Gives

I only get fields I need, and I skip headers, they are there already.

1 like
leviathanz's avatar

Hi, Thanks for comment. I already success to import data using csv file, but right now I got stack for update the existing data using csv.

leviathanz's avatar

Hi, I also created the update data one by one (without csv) before and it working. Right now I am try to learn how to update the data in bulk mode over csv

jlrdw's avatar

So here

public function updateDif($id)
    

Change to

public function updateDif()

Use the id from csv if that is the same id of record being updated.

Your update works, so you just have to find correct record to update, if not there, it's inserted.

Do a little trial and error, you almost have it.

2 likes

Please or to participate in this conversation.