StudentsImport.php 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace App\Imports;
  3. use App\Repositories\Models\User\Student;
  4. use Illuminate\Support\Str;
  5. use Maatwebsite\Excel\Concerns\OnEachRow;
  6. use Maatwebsite\Excel\Row;
  7. class StudentsImport implements OnEachRow
  8. {
  9. public function onRow(Row $row)
  10. {
  11. if ($row->getIndex() <= 1) return false;
  12. $sex = 0;
  13. switch ($row[3]) {
  14. case "男":
  15. case 'man':
  16. $sex = 1;
  17. break;
  18. case '女':
  19. case 'women':
  20. $sex = 2;
  21. break;
  22. }
  23. if (empty($row[1])) return false;
  24. $data = [
  25. 'turename' => $row[1],
  26. 'account' => $row[2],
  27. 'faculty' => $row[6],
  28. 'class' => $row[7],
  29. 'mobile' => $row[4],
  30. 'period' => $row[5],
  31. 'password' => Str::random(6),
  32. 'sex' => $sex,
  33. ];
  34. if (Student::query()->where('account', $row[2])->exists()) return false;
  35. Student::create($data);
  36. return true;
  37. }
  38. }