Advertisement
Guest User

UserImport

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