MMCT TEAM
Server IP : 103.191.208.50  /  Your IP : 216.73.216.53
Web Server : LiteSpeed
System : Linux orion.herosite.pro 4.18.0-553.53.1.lve.el8.x86_64 #1 SMP Wed May 28 17:01:02 UTC 2025 x86_64
User : celkcksm ( 1031)
PHP Version : 7.4.33
Disable Function : show_source, system, shell_exec, passthru, popen, exec
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON
Directory (0755) :  /home/celkcksm/demoadmin.ncriptech.com/../cms.ncriptech.com/app/Imports/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/celkcksm/demoadmin.ncriptech.com/../cms.ncriptech.com/app/Imports/StudentsImport.php
<?php

namespace App\Imports;

use Auth;
use Carbon\Carbon;
use App\Models\Student;
use App\Models\StudentEnroll;
use App\Models\EnrollSubject;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Validator;
use Maatwebsite\Excel\Concerns\ToCollection;
use Maatwebsite\Excel\Concerns\WithHeadingRow;

class StudentsImport implements ToCollection, WithHeadingRow
{
    protected $data;

    /**
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
    * @param array $row
    *
    * @return \Illuminate\Database\Eloquent\Model|null
    */
    public function collection(Collection $rows)
    {
        Validator::make($rows->toArray(), [
            '*.student_id' => 'required|unique:students,student_id',
            '*.admission_date' => 'required|date',
            '*.first_name' => 'required',
            '*.last_name' => 'required',
            '*.email' => 'required|email|unique:students,email',
            '*.gender' => 'required|integer',
            '*.dob' => 'required|date',
            '*.phone' => 'required',
        ])->validate();
  

        foreach ($rows as $row) {
            $student = Student::updateOrCreate(
                [
                'student_id'     => $row['student_id'],
                ],[
                'student_id'     => $row['student_id'],
                'batch_id'     => $this->data['batch'],
                'program_id'     => $this->data['program'],
                'admission_date'     => $row['admission_date'],
                'first_name'     => $row['first_name'],
                'last_name'     => $row['last_name'],
                'father_name'     => $row['father_name'],
                'mother_name'     => $row['mother_name'],
                'email'     => $row['email'],
                'password'      => Hash::make($row['student_id']),
                'password_text'     => Crypt::encryptString($row['student_id']),
                'gender'     => $row['gender'],
                'dob'     => $row['dob'],
                'phone'     => $row['phone'],
                'created_by'     => Auth::guard('web')->user()->id,
            ]);


            // Student Enroll
            $enroll = new StudentEnroll();
            $enroll->student_id = $student->id;
            $enroll->session_id = $this->data['session'];
            $enroll->semester_id = $this->data['semester'];
            $enroll->program_id = $this->data['program'];
            $enroll->section_id = $this->data['section'];
            $enroll->created_by = Auth::guard('web')->user()->id;
            $enroll->save();


            // Assign Subjects
            $enrollSubject = EnrollSubject::where('program_id', $this->data['program'])->where('semester_id', $this->data['semester'])->where('section_id', $this->data['section'])->first();
            
            if(isset($enrollSubject)){
                foreach($enrollSubject->subjects as $subject){
                    // Attach Subject
                    $enroll->subjects()->attach($subject->id);
                }
            }
        }
    }
}

MMCT - 2023