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/ImportUser.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 ImportUser implements ToModel, WithStartRow, WithValidation, WithHeadingRow, SkipsEmptyRows
{
	use Importable;
	public function startRow(): int
	{
		return 2;
	}

	public function rules(): array
	{
		return [
			"school_code" => "required|exists:schools,code",
			"class_code" => "required|exists:classes,code",
			"section_code" => "required|exists:sections,code",
			"first_name" => "required",
			"phone" => "required|unique:users|min:10",
			"gender" => "required",
			"password" => "required",
			"school_name" => "required",
			"father_name" => "required",
			"father_phone" => "required|numeric",
		];
	}

	public function model($row)
	{

		$classCode = Classes::where('code', trim($row['class_code']))->first();
		$sectionCode = Section::where('code', trim($row['section_code']))->first();
		$schoolCode = School::where('code', trim($row['school_code']))->first();

		// Register User ==============
		$hashed = Hash::make(trim($row['password']));
		$user_unique_id = uniqid();

		$user = new User();
		$user['user_unique_id'] = $user_unique_id;
		$user['admin_id'] = Auth()->user()->id;
		$user['name'] = trim($row['first_name']);
		$user['email'] = empty(trim($row['email'])) ? NULL : strtolower(trim($row['email']));
		$user['phone'] = trim($row['phone']);
		$user['fcm_token'] = NULL;
		$user['password'] = $hashed;
		$user['remember_token'] = NULL;
		$user['status'] = '1';
		$user['user_type'] = 'student';
		$user['created_at'] = date('Y-m-d H:i:s');

		// User Create
		if (!empty($user->save())) {
			// Assign Roles
			$role = Role::whereIn('slug', ['student'])->first();
			$user->roles()->attach($role);
			
			$uAdmisArr = new Users_admission();
			$uAdmisArr["user_unique_id"] = $user->user_unique_id;
			$uAdmisArr["enrollment_no"] = NULL;
			$uAdmisArr["school_name"] = trim(!empty($row['school_name']) ? $row['school_name'] : NULL);
			$uAdmisArr["inquiry_no"] = trim(!empty($row['inquiry_no']) ? $row['inquiry_no'] : NULL);
			$uAdmisArr["previous_admission_no"] = trim(!empty($row['previous_admission_no']) ? $row['previous_admission_no'] : NULL);
			$uAdmisArr["date_of_admission"] = Date::excelToDateTimeObject($row['date_admission'])->format('Y-m-d');
			$uAdmisArr["dob"] = trim(!empty($row['dob']) ? Date::excelToDateTimeObject($row['dob'])->format('Y-m-d') : NULL);
			$uAdmisArr["first_name"] = trim($row['first_name']);
			$uAdmisArr["last_name"] = trim(!empty($row['last_name']) ? $row['last_name'] : NULL);
			$uAdmisArr["gender"] = trim($row['gender']);
			$uAdmisArr["aadhar_no"] = trim(!empty($row['aadhar_no']) ? $row['aadhar_no'] : NULL);
			$uAdmisArr["address"] = trim(!empty($row['address']) ? $row['address'] : NULL);
			$uAdmisArr["town"] = trim(!empty($row['town']) ? $row['town'] : NULL);
			$uAdmisArr["district"] = trim(!empty($row['district']) ? $row['district'] : NULL);
			$uAdmisArr["pincode"] = trim(!empty($row['pincode']) ? $row['pincode'] : NULL);
			$uAdmisArr["state"] = trim(!empty($row['state']) ? $row['state'] : NULL);
			$uAdmisArr["created_at"] = date('Y-m-d H:i:s');

			$admission = $uAdmisArr->save();

			// print_r($user['phone']);
			if ($admission) {
				$uAdmisArr['enrollment_no'] = $classCode->code . '-' . $sectionCode->code . '-' . $uAdmisArr->id;
				$uAdmisArr->save();
				// Attech School
				$userSchool = array(
					'user_id' => $user->id,
					'school_id' => $schoolCode->id
				);

				$schoolarr = array();
				array_push($schoolarr, $userSchool);
				$user->school()->attach($schoolarr);

				$classSection = array(
					'user_id' => $user->id,
					'classes_id' => $classCode->id,
					'section_id' => $sectionCode->id
				);
				$classArr = array();
				array_push($classArr, $classSection);
				$user->class()->attach($classArr);

				// Check Existing Parents phone ================
				$parent = Guardian::where('phone', $row['father_name'])->first();

				if (!empty($parent)) {
					$user->guardian()->attach($parent->id);
				} else {
					// Users Parents Form
					$uParentArr = new Guardian();
					$uParentArr["user_unique_id"] = $user->user_unique_id;
					$uParentArr["guardian_name"] = $row['father_name'];
					$uParentArr["phone"] = $row['father_phone'];
					$uParentArr["father_name"] = $row['father_name'];
					$uParentArr["created_at"] = date('Y-m-d H:i:s');
					// Save Parents attached
					$_parent = $uParentArr->save();
					if ($_parent) {
						$user->guardian()->attach($uParentArr->id);

					}
				}

				// END Existing Parents phone ==================
			}

			// End Loop
		}
	}
}

Anon7 - 2021