Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Imports;
- use App\User;
- use Maatwebsite\Excel\Concerns\ToModel;
- use Maatwebsite\Excel\Concerns\WithHeadingRow;
- class UserImport implements ToModel, WithHeadingRow
- {
- /**
- * @param array $row
- *
- * @return \Illuminate\Database\Eloquent\Model|null
- */
- public function model(array $row)
- {
- $user = User::create([
- 'name' => $row['name'],
- 'email' => $row['email'],
- 'password' =>bcrypt('omni123'),
- 'department_id' =>$row['department_id'],
- 'role' => $row['role'],
- 'username' => $row['username'],
- 'unit_id' => $row['unit_id'],
- 'status' => $row['status'],
- 'birth_date' => $row['birth_date'],
- 'birth_place' => $row['birth_place'],
- 'address' => $row['address'],
- 'mobile_phone' => $row['mobile_phone'],
- 'nik' => $row['nik']
- ]);
- if($row['role'] == 'administrator'){
- $user->assignRole('administrator');
- } elseif($row['role'] == 'direct_manager'){
- $user->assignRole('direct_manager');
- } elseif($row['role'] == 'user'){
- $user->assignRole('user');
- }
- }
- public function headingRow(): int
- {
- return 1;
- }
- public function transformDate($value, $format = 'Y-m-d')
- {
- try{
- return \Carbon\Carbon::instance(\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value));
- } catch(\ErrorException $e) {
- return \Carbon\Carbon::createFromFormat($format, $value);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement