123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <?php
- namespace App\Imports;
- use App\Models\User;
- use Illuminate\Support\Collection;
- use Illuminate\Validation\Rule;
- use Maatwebsite\Excel\Concerns\ToCollection;
- use Maatwebsite\Excel\Concerns\ToModel;
- use Maatwebsite\Excel\Concerns\Importable;
- use Maatwebsite\Excel\Concerns\WithBatchInserts;
- use Maatwebsite\Excel\Concerns\WithHeadingRow;
- use Maatwebsite\Excel\Concerns\WithValidation;
- use Maatwebsite\Excel\Concerns\WithStartRow;
- class UsersImport implements ToCollection,WithStartRow,WithValidation
- {
- use Importable;
- /**
- * @param array $row
- *
- * @return \Illuminate\Database\Eloquent\Model|null
- */
- public function collection(Collection $rows)
- {
- foreach ($rows as $row)
- {
- // var_dump($rows);
- User::create([
- 'name' => $row[1],
- 'cre_num' => $row[2],
- 'telphone' => $row[3],
- 'address' => $row[4],
- ]);
- }
- }
- public function startRow(): int
- {
- return 2;
- }
- public function rules(): array
- {
- return [
- 'name' => Rule::in(['required', 'unique:users', 'max:64']),
- 'cre_num' => Rule::in(['required', 'unique:users', 'max:18']),
- 'telphone' => Rule::in(['required', 'max:18']),
- ];
- }
- // public function batchSize(): int
- // {
- // return 1000;
- // }
- // public function headingRow(): int
- // {
- // return 3;
- // }
- }
|