Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Maatwebsite/Laravel-Excel Update if exist

I'm using This Package Maatwebsite/Laravel-Excel to import data from excel file

what I wanna do is check if the column exists before import data from excel file,

if so, then update otherwise insert

Model:

<?php

namespace App\Imports;

use App\Medicine;
use Carbon\Carbon;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Imports\HeadingRowFormatter;

HeadingRowFormatter::default('none');
class MedicineImport implements ToModel, WithHeadingRow
{
    protected $company_id;

    public function __construct($company_id)
    {
        $this->company_id = $company_id;
    }
    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function model(array $row)
    {
        $expire_date = empty($row['Expire Date']) ? $row['Expire Date'] : Carbon::instance(\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row['Expire Date']));
        return new Medicine([
            'name'         => $row['Name'],
            'price'        => $row['Price'],
            'expire_date'  => $expire_date,
            'company_id'    => $this->company_id,

        ]);
    }
}

Controller:

$company = Company::where('id',$id)->first();
    $medicines=DB::table('medicines')->where('company_id', $id)->delete();
    $company_id= $id;
    Excel::import(new MedicineImport($company_id),request()->file('file'));

    return redirect()->route('company.medicine.index',$company_id);

any ideas to do this?

like image 870
Jimmy Phill Avatar asked Nov 02 '25 06:11

Jimmy Phill


2 Answers

You need to check just before insert, example:

public function model(array $row)
{
    $exists = Medicine::where('name',$row['Name'])->where('company_id',$this->company_id)->first();
    if ($exists) {
        //LOGIC HERE TO UPDATE
        return null;
    }
    
    $expire_date = empty($row['Expire Date']) ? $row['Expire Date'] : Carbon::instance(\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($row['Expire Date']));
    return new Medicine([
        'name'         => $row['Name'],
        'price'        => $row['Price'],
        'expire_date'  => $expire_date,
        'company_id'    => $this->company_id,

    ]);
}
like image 133
Itamar Garcia Avatar answered Nov 03 '25 21:11

Itamar Garcia


Itamar Garcia's answer is really a bad idea, I tried it in production and faced too many issues, loading took forever to finish, started getting lock timeout error etc. Anyway I ended up using WithUpserts, sorry to post this in a separate answer, stupid stackoverflow policy prohibits me from commenting on other answers until I reach 50 reputation.

use Maatwebsite\Excel\Concerns\WithUpserts;

class SubscribersImport implements ToModel, WithUpserts {
    /**/
    public function uniqueBy(){
        /**/
        return "email";
        /**/
    }
}
like image 24
Austyn Wilson Avatar answered Nov 03 '25 22:11

Austyn Wilson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!