Search code examples
excellaravellaravel-livewire

how can I update the table for existing data while importing through Maatwebsite in laravel


I want to check for existing data while importing through Maatwebsite in laravel and any existing data is found just update it instead of inserting new row.

Currently I am doing this

namespace App\Imports;

use App\Models\PatientData;
use Carbon\Carbon;
use Illuminate\Contracts\Session\Session;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class PatientsImport implements ToModel,WithHeadingRow
{
    /**
    * @param Collection $collection
    */
    public function model(array $row)
    {

        try{
            return new PatientData([
                'file_number'=>$row['file_number'],
                'patient_name' =>$row['patient_name'],
                'mobile_number' => $row['mobile_number'],
                'date'=> Carbon::parse($row['date'])->format('Y-m-d'),
                'sms_status'=>session()->get('msg_type'),


            ]);
        }catch(\Exception $e)
        {
            Log::channel('custom')->info('We are Facing this error while uploading',['error'=>$e->getMessage()]);
        }
    }
}


Solution

  • Just adding few conditions in my code resolved my issue.
    
    <?php
    
    namespace App\Imports;
    
    use App\Models\PatientData;
    use Carbon\Carbon;
    use Illuminate\Contracts\Session\Session;
    use Illuminate\Support\Collection;
    use Maatwebsite\Excel\Concerns\ToCollection;
    use Illuminate\Support\Facades\Hash;
    use Illuminate\Support\Facades\Log;
    use Maatwebsite\Excel\Concerns\ToModel;
    use Maatwebsite\Excel\Concerns\WithHeadingRow;
    
    class PatientsImport implements ToModel,WithHeadingRow
    {
        /**
        * @param Collection $collection
        */
        public function model(array $row)
        {
    
            try{
                //chcking for existing data
                $data_existing = PatientData::where('file_number',$row['file_number'])->whereDate('date', Carbon::parse($row['date'])->format('Y-m-d'))
                ->where('mobile_number',$row['mobile_number'])->first();
                if($data_existing)
                {
                    return;
    
                }
                return new PatientData([
                    'file_number'=>$row['file_number'],
                    'patient_name' =>$row['patient_name'],
                    'mobile_number' => $row['mobile_number'],
                    'date'=> Carbon::parse($row['date'])->format('Y-m-d'),
                    'sms_status'=>session()->get('msg_type'),
    
    
                ]);
            }catch(\Exception $e)
            {
                Log::channel('custom')->info('We are Facing this error while uploading',['error'=>$e->getMessage()]);
            }
        }
    }