Skip to content

Commit

Permalink
Add students excel import
Browse files Browse the repository at this point in the history
Students data can be imported from excel sheet
  • Loading branch information
changeweb committed May 13, 2019
1 parent d4de250 commit 640afed
Show file tree
Hide file tree
Showing 4 changed files with 446 additions and 2 deletions.
55 changes: 55 additions & 0 deletions app/Imports/StudentsImport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace App\Imports;

use App\User;
use Illuminate\Support\Facades\Hash;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithChunkReading;
use Maatwebsite\Excel\Concerns\WithBatchInserts;

class StudentsImport implements ToModel
{
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
return new User([
'name' => $row['name'],
'email' => $row['email'],
'password' => Hash::make($row['password']),
'active' => 1,
'role' => 'student',
'school_id' => auth()->user()->school_id,
'code' => auth()->user()->code,
'student_code' => auth()->user()->school_id.date('y').substr(number_format(time() * mt_rand(), 0, '', ''), 0, 5),
'address' => $row['address'],
'about' => $row['about'],
'pic_path' => '',
'phone_number' => $row['phone_number'],
'verified' => 1,
'section_id' => function () {
if($row['class'] != 0 && $row['section'] != 0){
$class_id = Myclass::bySchool()->where()->pluck('id');
return Section::where('class_id', $class_id)->pluck('id');
}
},
'blood_group' => $row['blood_group'],
'nationality' => $row['nationality'],
'gender' => $row['gender'],
]);
}

public function chunkSize(): int
{
return 1000;
}

public function batchSize(): int
{
return 200;
}
}
2 changes: 1 addition & 1 deletion app/Services/User/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public function storeStudent($request){
$tb->role = 'student';
$tb->active = 1;
$tb->school_id = auth()->user()->school_id;
$tb->code = auth()->user()->code;
$tb->code = auth()->user()->code;// School Code
$tb->student_code = auth()->user()->school_id.date('y').substr(number_format(time() * mt_rand(), 0, '', ''), 0, 5);
$tb->gender = $request->gender;
$tb->blood_group = $request->blood_group;
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"laravel/framework": "5.5.*",
"laravel/passport": "^4.0",
"laravel/tinker": "~1.0",
"maatwebsite/excel": "^3.1",
"maddhatter/laravel-fullcalendar": "^1.3",
"mavinoo/laravel-batch": "2.0",
"renatomarinho/laravel-page-speed": "^1.8"
Expand Down
Loading

0 comments on commit 640afed

Please sign in to comment.