UsersImport.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Imports;
  3. use App\Models\User;
  4. use Illuminate\Support\Collection;
  5. use Illuminate\Validation\Rule;
  6. use Maatwebsite\Excel\Concerns\ToCollection;
  7. use Maatwebsite\Excel\Concerns\ToModel;
  8. use Maatwebsite\Excel\Concerns\Importable;
  9. use Maatwebsite\Excel\Concerns\WithBatchInserts;
  10. use Maatwebsite\Excel\Concerns\WithHeadingRow;
  11. use Maatwebsite\Excel\Concerns\WithValidation;
  12. use Maatwebsite\Excel\Concerns\WithStartRow;
  13. class UsersImport implements ToCollection,WithStartRow,WithValidation
  14. {
  15. use Importable;
  16. /**
  17. * @param array $row
  18. *
  19. * @return \Illuminate\Database\Eloquent\Model|null
  20. */
  21. public function collection(Collection $rows)
  22. {
  23. foreach ($rows as $row)
  24. {
  25. // var_dump($rows);
  26. User::create([
  27. 'name' => $row[1],
  28. 'cre_num' => $row[2],
  29. 'telphone' => $row[3],
  30. 'address' => $row[4],
  31. ]);
  32. }
  33. }
  34. public function startRow(): int
  35. {
  36. return 2;
  37. }
  38. public function rules(): array
  39. {
  40. return [
  41. 'name' => Rule::in(['required', 'unique:users', 'max:64']),
  42. 'cre_num' => Rule::in(['required', 'unique:users', 'max:18']),
  43. 'telphone' => Rule::in(['required', 'max:18']),
  44. ];
  45. }
  46. // public function batchSize(): int
  47. // {
  48. // return 1000;
  49. // }
  50. // public function headingRow(): int
  51. // {
  52. // return 3;
  53. // }
  54. }