ldc-com-app-bo/app/Imports/AppUserImport.php
2023-03-31 16:53:04 +02:00

55 lines
1.2 KiB
PHP

<?php
namespace App\Imports;
use App\Enums\AppUserRolesEnum;
use App\Models\AppUser;
use Illuminate\Support\Str;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithUpserts;
use Maatwebsite\Excel\Concerns\WithCustomCsvSettings;
class AppUserImport implements ToModel, WithUpserts, WithCustomCsvSettings
{
/**
* @return string|array
*/
public function uniqueBy()
{
return 'email';
}
/**
* @return array
*/
public function getCsvSettings(): array
{
return [
'delimiter' => ";"
];
}
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
if (!isset($row[0]) || !isset($row[1]) || !isset($row[2]) || !isset($row[3])) {
return null;
}
$user = new AppUser([
'email' => $row[3],
'lastname' => ucfirst(strtolower($row[1])),
'firstname' => ucfirst(strtolower($row[2])),
'password' => Str::random(10),
'status' => 1,
'role' => AppUserRolesEnum::user(),
]);
$user->saveOrFail();
return $user;
}
}