1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Imports; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use App\Models\Examination_student; |
7
|
|
|
use Maatwebsite\Excel\Concerns\ToModel; |
8
|
|
|
use Maatwebsite\Excel\Validators\Failure; |
9
|
|
|
use Maatwebsite\Excel\Concerns\Importable; |
10
|
|
|
use Maatwebsite\Excel\Concerns\SkipsErrors; |
11
|
|
|
use Maatwebsite\Excel\Concerns\WithMapping; |
12
|
|
|
use Maatwebsite\Excel\Concerns\SkipsOnError; |
13
|
|
|
use Maatwebsite\Excel\Concerns\WithStartRow; |
14
|
|
|
use Maatwebsite\Excel\Concerns\SkipsFailures; |
15
|
|
|
use Maatwebsite\Excel\Concerns\SkipsOnFailure; |
16
|
|
|
use Maatwebsite\Excel\Concerns\WithHeadingRow; |
17
|
|
|
use Maatwebsite\Excel\Concerns\WithValidation; |
18
|
|
|
use Maatwebsite\Excel\Concerns\WithBatchInserts; |
19
|
|
|
use Maatwebsite\Excel\Concerns\WithChunkReading; |
20
|
|
|
|
21
|
|
|
class ExaminationStudentsImport implements ToModel, WithStartRow, WithHeadingRow, WithChunkReading, WithBatchInserts , WithValidation ,WithMapping, SkipsOnFailure, SkipsOnError |
22
|
|
|
{ |
23
|
|
|
use Importable , SkipsFailures, SkipsErrors; |
|
|
|
|
24
|
|
|
|
25
|
|
|
public function __construct($year,$grade) |
26
|
|
|
{ |
27
|
|
|
$this->year = $year; |
|
|
|
|
28
|
|
|
$this->grade = $grade; |
|
|
|
|
29
|
|
|
} |
30
|
|
|
/** |
31
|
|
|
* @return int |
32
|
|
|
*/ |
33
|
|
|
public function startRow(): int |
34
|
|
|
{ |
35
|
|
|
return 2; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return int |
40
|
|
|
*/ |
41
|
|
|
public function headingRow(): int |
42
|
|
|
{ |
43
|
|
|
return 1; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function chunkSize(): int |
47
|
|
|
{ |
48
|
|
|
return 10000; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function batchSize(): int |
52
|
|
|
{ |
53
|
|
|
return 10000; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function map($row): array |
57
|
|
|
{ |
58
|
|
|
$row['b_date'] = $this->transformDateTime($row['b_date']); |
59
|
|
|
return $row; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
private function transformDateTime(string $value, string $format = 'm/d/Y') |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
try{ |
66
|
|
|
$date = date_create_from_format('m/d/Y', $value); |
67
|
|
|
if(gettype($date)=='boolean'){ |
68
|
|
|
$date = date_create_from_format('Y-m-d', $value); |
69
|
|
|
} |
70
|
|
|
$date = date_format($date, 'Y-m-d'); |
71
|
|
|
return $date; |
72
|
|
|
}catch(\Exception $e){ |
73
|
|
|
$error = \Illuminate\Validation\ValidationException::withMessages([]); |
74
|
|
|
$failure = new Failure(2, 'remark', [0 => 'The given date is wrong']); |
75
|
|
|
$failures = [0 => $failure]; |
76
|
|
|
new \Maatwebsite\Excel\Validators\ValidationException($error, $failures); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param Collection $collection |
83
|
|
|
*/ |
84
|
|
|
public function model(array $row) |
85
|
|
|
{ |
86
|
|
|
|
87
|
|
|
$insertData = array( |
88
|
|
|
'st_no' => $row['st_no'], |
89
|
|
|
'stu_no' => $row['stu_no'], |
90
|
|
|
"f_name" => $row['f_name'], |
91
|
|
|
"medium" => $row['medium'], |
92
|
|
|
"gender" => $row['gender'], |
93
|
|
|
"b_date" => $row['b_date'], |
94
|
|
|
"a_income" => $row['a_income'] ? $row['a_income'] : 0 , |
95
|
|
|
"grade" => $this->grade, |
96
|
|
|
'year' => $this->year, |
97
|
|
|
"schoolid" => $row['schoolid'], |
98
|
|
|
"spl_need" => $row['spl_need'], |
99
|
|
|
"pvt_address" => $row['pvt_address'], |
100
|
|
|
"disability_type" => $row['disability_type'], |
101
|
|
|
"disability" => $row['disability'], |
102
|
|
|
"sp_center" => $row['sp_center'] |
103
|
|
|
); |
104
|
|
|
Examination_student::insertData($insertData); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function rules(): array |
108
|
|
|
{ |
109
|
|
|
return [ |
110
|
|
|
'b_date' => 'date|required', |
111
|
|
|
'schoolid' => 'exists:institutions,code' |
112
|
|
|
]; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|