12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- <?php
- namespace App\Imports;
- use App\Repositories\Models\User\Student;
- use Illuminate\Support\Str;
- use Maatwebsite\Excel\Concerns\OnEachRow;
- use Maatwebsite\Excel\Row;
- class StudentsImport implements OnEachRow
- {
- public function onRow(Row $row)
- {
- if ($row->getIndex() <= 1) return false;
- $sex = 0;
- switch ($row[3]) {
- case "男":
- case 'man':
- $sex = 1;
- break;
- case '女':
- case 'women':
- $sex = 2;
- break;
- }
- if (empty($row[1])) return false;
- $data = [
- 'turename' => $row[1],
- 'account' => $row[2],
- 'faculty' => $row[6],
- 'class' => $row[7],
- 'mobile' => $row[4],
- 'period' => $row[5],
- 'password' => Str::random(6),
- 'sex' => $sex,
- ];
- if (Student::query()->where('account', $row[2])->exists()) return false;
- Student::create($data);
- return true;
- }
- }
|