Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.63 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Imports;
  4.  
  5. use App\User;
  6. use Maatwebsite\Excel\Concerns\ToModel;
  7. use Maatwebsite\Excel\Concerns\WithHeadingRow;
  8.  
  9. class UserImport implements ToModel, WithHeadingRow
  10. {
  11.     /**
  12.     * @param array $row
  13.     *
  14.     * @return \Illuminate\Database\Eloquent\Model|null
  15.     */
  16.     public function model(array $row)
  17.     {
  18.         $user = User::create([
  19.             'name' => $row['name'],
  20.             'email' => $row['email'],
  21.             'password' =>bcrypt('omni123'),
  22.             'department_id' =>$row['department_id'],
  23.             'role' => $row['role'],
  24.             'username' => $row['username'],
  25.             'unit_id' => $row['unit_id'],
  26.             'status' => $row['status'],
  27.             'birth_date' => $row['birth_date'],
  28.             'birth_place' => $row['birth_place'],
  29.             'address' => $row['address'],
  30.             'mobile_phone' => $row['mobile_phone'],
  31.             'nik' => $row['nik']
  32.         ]);
  33.  
  34.         if($row['role'] == 'administrator'){
  35.             $user->assignRole('administrator');
  36.         } elseif($row['role'] == 'direct_manager'){
  37.             $user->assignRole('direct_manager');
  38.         } elseif($row['role'] == 'user'){
  39.             $user->assignRole('user');
  40.         }
  41.     }
  42.  
  43.     public function headingRow(): int
  44.     {
  45.         return 1;
  46.     }
  47.  
  48.     public function transformDate($value, $format = 'Y-m-d')
  49.     {
  50.         try{
  51.             return \Carbon\Carbon::instance(\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value));
  52.         } catch(\ErrorException $e) {
  53.             return \Carbon\Carbon::createFromFormat($format, $value);
  54.         }
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement