IMMREX7

aku nok ndi : /home/spdtg/www/schoolmis/app/Http/Controllers/School/
File Up :
aku nok ndi : /home/spdtg/www/schoolmis/app/Http/Controllers/School/ExcelUploadController.php

<?php

namespace App\Http\Controllers\School;

use Illuminate\Http\Request;
use App\Imports\StudentImport;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;
use Auth;
use DB;

class ExcelUploadController extends SchoolController {

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index() {
        return view('schools.students.excelupload');
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create() {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request) {
//        $rules = [
//            'excelfile'=>'required'
//        ];
//        $messages = [];
//        $this->validate($request, $rules,$messages);
        ini_set('memory_limit', -1);
        ini_set('max_execution_time', '300');
        $school = \App\School::where('idSchool', '=', Auth::guard('school')->user()->idSchool)->first();
        if ($request->hasFile('excelfile')) {
            $path = $request->file('excelfile')->getRealPath();
            $import = new StudentImport();
            Excel::import($import, $request->file('excelfile'));
            $row = 0;
	    if (count($import->getData()) > 0) {
                foreach ($import->getData() as $key => $dr) {
                    $row++;
                    $value = (object) $dr;
                    $session = \App\FinancialYear::where('financialYearName', '=', $value->session)->where('idSchool', '=', $school->idSchool)->first();
                    if($session == null){
                        flash('Session is invalid or not correct in row no.'.$row);
                        return redirect('school/stdexcelupload');
                    }
                    $class = \App\ClassM::where('className', '=', $value->classname)->where('idSchool', '=', $school->idSchool)->first();
                    //dd($class);
                    if($class == null){
                        flash('Class is invalid or not correct in row no.'.$row);
                        return redirect('school/stdexcelupload');
                    }
                    $section = \App\Section::where('idSchool', '=', $school->idSchool)
                                    ->where('idClass', '=', $class->idClass)
                                    ->where('sectionName', '=', $value->section)->first();
                    if($section == null){
                        flash('Section is invalid or not correct in row no.'.$row);
                        return redirect('school/stdexcelupload');
                    }                
                    // dd($section);
                    //   $resState = \App\State::where('idState', '=', $value->resstate)->first();
                    //   $lastschState = \App\State::where('idState', '=',$value->lastschoolstate)->first();
                    $next_no = next_admno();
                    $sch = substr($school->schoolName, 0, 4);
                    $adm_no = $sch . '000' . $next_no;
                    try{
                        $arr[] = [
                            'idSchool' => $school->idSchool,
                            'idFinancialYear' => $session->idFinancialYear,
                            'admissionNo' => $adm_no,
                            'ecno' => $value->ecNo,
                            'firstName' => $value->firstName,
                            'middleName' => $value->middleName,
                            'lastName' => $value->lastName,
                            'idClass' => $class->idClass,
                            'idSection' => $section->idSection,
                            'studentDob' => $value->studentDob,
                            'studentPob' => $value->studentPob,
                            'gender' => $value->gender,
                            'aadhaarNo' => $value->aadhaarNo,
                            'religion' => $value->religion,
                            'studentType' => $value->studentType,
                            'studentUID' => $value->studentUID,
                            'motherTounge' => $value->motherTounge,
                            'grNo' => $value->grNo,
                            'saralNo' => $value->saralNo,
                            'bloodGroup' => $value->bloodGroup,
                            'resAddress' => $value->resAddress,
                            'resCity' => $value->resCity,
                            'resPincode' => $value->resPincode,
                            'landmark' => $value->landmark,
                            'resState' => $value->resState,
                            'resTelephone' => $value->resTelephone,
                            'lastSchool' => $value->lastSchool,
                            'board' => $value->board,
                            'percentage' => $value->percentage,
                            'lastSchoolAddress' => $value->lastSchoolAddress,
                            'lastSchoolCity' => $value->lastSchoolCity,
                            'lastSchoolPhone' => $value->lastSchoolPhone,
                            'lastSchoolPincode' => $value->lastSchoolPincode,
                            'lastSchoolState' => $value->lastSchoolState,
                            'reasonForChange' => $value->reasonForChange,
                            'contactPerson' => $value->contactPerson,
                            'contactPersonAddress' => $value->contactPersonAddress,
                            'contactPersonRelation' => $value->contactPersonRelation,
                            'contactPersonTel' => $value->contactPersonTel,
                            'contactPersonMobile' => $value->contactPersonMobile,
                            'food' => $value->food,
                            'transport' => $value->transport,
                            'learningDisability' => $value->learningDisability,
                            'otherRequirement' => $value->otherRequirement,
                            'ralationship_one' => $value->ralationship_one,
                            'father_fname' => $value->father_fname,
                            'father_lname' => $value->father_lname,
                            'father_dob' => $value->father_dob,
                            'father_qualification' => $value->father_qualification,
                            'father_occupation' => $value->father_occupation,
                            'father_designation' => $value->father_designation,
                            'father_company' => $value->father_company,
                            'father_workloc' => $value->father_workloc,
                            'father_panno' => $value->father_panno,
                            'father_telephone' => $value->father_telephone,
                            'father_mobile' => $value->father_mobile,
                            'father_aadhaar' => $value->father_aadhaar,
                            'father_email' => $value->father_email,
                            'relationship_two' => $value->relationship_two,
                            'mother_fname' => $value->mother_fname,
                            'mother_lname' => $value->mother_lname,
                            'mother_dob' => $value->mother_dob,
                            'mother_qualification' => $value->mother_qualification,
                            'mother_occupation' => $value->mother_occupation,
                            'mother_designation' => $value->mother_designation,
                            'mother_company' => $value->mother_company,
                            'mother_workloc' => $value->mother_workloc,
                            'mother_panno' => $value->mother_panno,
                            'mother_telephone' => $value->mother_telephone,
                            'mother_mobile' => $value->mother_mobile,
                            'mother_aadhaar' => $value->mother_aadhaar,
                            'mother_email' => $value->mother_email
                        ];
                        $par1[] = [
                            'idSchool' => $school->idSchool,
                            'mobile' => $value->father_mobile,
                            'pwd' => '123456',
                            'password' => bcrypt('123456'),
                        ];
                        $par2[] = [
                            'idSchool' => $school->idSchool,
                            'mobile' => $value->mother_mobile,
                            'pwd' => '123456',
                            'password' => bcrypt('123456'),
                        ];
                    }catch (\Exception $e) {
                        // An error occured; cancel the transaction...
                        DB::rollback();
                        // and throw the error again.
                        flash('Error in row .'.$row. ' '.$e->getMessage());
                    }
                    
                }


                if (!empty($arr)) {
                    try {
                        DB::beginTransaction();
                        foreach (array_chunk($arr, 500) as $t) {
                            \DB::table('students')->insert($t);
                        }
                        foreach (array_chunk($par1, 500) as $p1) {
                            \DB::table('parents')->insert($p1);
                        }
                        foreach (array_chunk($par2, 500) as $p2) {
                            \DB::table('parents')->insert($p2);
                        }
                        DB::commit();
                        flash('Record saved successfully.');
                    } catch (\Exception $e) {
                        // An error occured; cancel the transaction...
                        DB::rollback();
                        // and throw the error again.
                        flash('Error in row .'.$row. ' '.$e->getMessage());
                    }
                }
            }
            return redirect('school/stdexcelupload');
        }
        flash('Request data does not have any files to import.');
        return redirect('school/stdexcelupload');
    }

}

Copyright © 2021 - 2025 IMMREX7