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 AppModelsSignage;

use IlluminateDatabaseEloquentModel;

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

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

this is my Imports function :

<?php

namespace AppImports;

use AppModelsSignageExchangerate;
use MaatwebsiteExcelRow;
use MaatwebsiteExcelConcernsOnEachRow;


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.

Answer

Check this link on laravel documentation UpdateOrCreate: So in your updateOrCreate method you have to add an extra array(first array) that help Model to find if a model exist or not for example:

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