Search code examples
phpdatabaselaravelimportmaatwebsite-excel

How to update existing data from import CSV ? with laravel


i have some issue with my import csv in laravel with package MaatWesbite, how to i update the existing data with the import CSV ??

this is my controller :

public function storeData(Request $request)
{
    $this->validate($request, [
        'file' => 'required|mimes:csv,txt'
    ]);

    // $date = new DateTime;
    $exchange_rate = new ExchangeRate();
    if ($request->hasFile('file')) {
        $file = $request->file('file');
        $nama_file = date('Y-m-d').$file->getClientOriginalName();
        $destinasi = 'public/kurs/';
        $file->move($destinasi, $nama_file);
        Excel::import(new ExchangeRateImport, public_path('/public/kurs/' . $nama_file));

        Display::where('display_type', ExchangeRate::class)
            ->where('display_id', $exchange_rate->id)
            ->delete();

        if (isset($request->device)) {
            foreach ($request->device as $device) {
                $display = new Display();
                $display->device_id = $device;
                $display->display_type = ExchangeRate::class;
                $display->display_id = $exchange_rate->id;
                $display->save();
            }
        }
    }

    return redirect()->route('signage.exchange-rate.index')->with('success', __('backend.exchange_rate') . __('backend.added'));
}

and this is my model :

<?php

namespace App\Models\Signage;

use Illuminate\Database\Eloquent\Model;

class ExchangeRate extends Model
{
    protected $table = 'signage_exchange_rates';

    protected $fillable = [
        'country', 'type', 'bank_buy', 'bank_sell'
    ];
}

this is my Imports function :

<?php

namespace App\Imports;

use App\Models\Signage\Exchangerate;
use Maatwebsite\Excel\Row;
use Maatwebsite\Excel\Concerns\OnEachRow;


class ExchangeRateImport implements OnEachRow
{
    public function onRow(Row $row)
    {
        $row = $row->toArray();

        if (!isset($row[0])) {
            return null;
        }
        
        $exchange = ExchangeRate::updateOrCreate([
            'country' => preg_replace("/[^a-zA-Z0-9]/", "", $row[0]),
            'type' => $row[1],
            'bank_buy' => $row[2],
            'bank_sell' => $row[3],
        ]);

        if (! $exchange->wasRecentlyCreated) {
            $exchange->update([
                'country' => preg_replace("/[^a-zA-Z0-9]/", "", $row[0]),
                'type' => $row[1],
                'bank_buy' => $row[2],
                'bank_sell' => $row[3],
            ]);
        }
    }
}

i already do that but when the import success, the csv doesn't replace existing data always insert new data.


Solution

  • Maybe i get here too late but, this package have his own Upserts (Update or Insert).

    Your ExchangeImport file would look like this:

    
    namespace App\Imports;
    
    use App\Models\Signage\Exchangerate;
    use Maatwebsite\Excel\Row;
    use Maatwebsite\Excel\Concerns\OnEachRow;
    use Maatwebsite\Excel\Concerns\WithUpserts;
    
    class ExchangeRateImport implements OnEachRow, WithUpserts
    {
    
        /**
         * @return string|array
         */
        public function uniqueBy()
        {
            return 'country';
        }
    
        /**
        * @param array $row
        *
        * @return \Illuminate\Database\Eloquent\Model|null
        */
        public function model(array $row)
        {
            return new ExchangeRate([
                'country' => preg_replace("/[^a-zA-Z0-9]/", "", $row[0]),
                'type' => $row[1],
                'bank_buy' => $row[2],
                'bank_sell' => $row[3],
            ]);
        }
    }