KGRKJGETMRETU895U-589TY5MIGM5JGB5SDFESFREWTGR54TY
Server : Apache/2.4.41 (Ubuntu)
System : Linux vmi1525618.contaboserver.net 5.4.0-105-generic #119-Ubuntu SMP Mon Mar 7 18:49:24 UTC 2022 x86_64
User : www-data ( 33)
PHP Version : 8.2.12
Disable Function : NONE
Directory :  /var/www/paras.theinteractive.in/app/Imports/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //var/www/paras.theinteractive.in/app/Imports/ImportParent.php
<?php

namespace App\Imports;

use App\Models\Role;
use App\Models\User;
use App\Models\School;
use App\Models\Classes;
use App\Models\Section;
use App\Models\Guardian;
use App\Models\Users_admission;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Session;
use Maatwebsite\Excel\Concerns\ToModel;
use PhpOffice\PhpSpreadsheet\Shared\Date;
use Maatwebsite\Excel\Concerns\Importable;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithStartRow;
use Maatwebsite\Excel\Concerns\SkipsEmptyRows;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithValidation;

class ImportParent implements ToModel, WithStartRow, WithValidation, WithHeadingRow, SkipsEmptyRows
{
    use Importable;
    public function startRow(): int
    {
        return 2;
    }

    public function rules(): array
    {
        return [
            "id" => "required|exists:users,id",
            "enrollment_no" => "required|exists:users_admissions,enrollment_no",
            "father_name" => "required",
            "father_phone" => "required|numeric"
        ];
    }

    public function model($row)
    {
        // Find User ==============
        $query = User::with('guardian', 'school', 'class', 'section');
        $query->Join('users_admissions', 'users_admissions.user_unique_id', '=', 'users.user_unique_id');
        $query->where('users.id', $row['id']);
        $user = $query->first(['users.*']);
        $parent = $user->guardian()->first();

        // Parent Update
        $p_data = array(
            'guardian_name' => $row['father_name'],
            'father_name' => $row['father_name'],
            'email' => $row['father_email'],
            'phone' => $row['father_phone'],
            'father_occupation' => $row['father_occupation'],
            'mother_name' => $row['mother_name'],
            'mother_email' => $row['mother_email'],
            'mother_phone' => $row['mother_phone'],
            'mother_occupation' => $row['mother_occupation'],
            'address' => $row['parent_address'],
            'town' => $row['parent_town'],
            'district' => $row['parent_district'],
            'pincode' => $row['parent_pincode'],
            'state' => $row['parent_state']
        );
        if (!empty($parent)) {
            Guardian::where('id', $parent->id)->update($p_data);
        }
    }
}

Anon7 - 2021