ldc-com-app-bo/app/Imports/AppUserImport.php

65 lines
1.7 KiB
PHP
Raw Normal View History

2023-03-31 15:24:32 +02:00
<?php
namespace App\Imports;
2023-07-13 08:34:54 +00:00
use App\Mail\AccountCreated;
2023-03-31 15:24:32 +02:00
use App\Enums\AppUserRolesEnum;
use App\Models\AppUser;
2023-07-13 08:34:54 +00:00
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
2023-03-31 15:24:32 +02:00
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(): string|array
2023-03-31 15:24:32 +02:00
{
return 'email';
}
/**
* @return array
*/
public function getCsvSettings(): array
{
return [
'delimiter' => ";"
];
}
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row): \Illuminate\Database\Eloquent\Model|null
2023-03-31 15:24:32 +02:00
{
if (!isset($row[0]) || !isset($row[1]) || !isset($row[2]) || !isset($row[3])) {
return null;
}
$password = Hash::make(Str::random(10));
$user = new AppUser([
'email' => $row[3],
'lastname' => ucfirst(strtolower($row[1])),
2023-03-31 15:24:32 +02:00
'firstname' => ucfirst(strtolower($row[2])),
'password' => $password,
'status' => 1,
'role' => AppUserRolesEnum::user,
2023-03-31 15:24:32 +02:00
]);
$user->saveOrFail();
2025-05-20 16:20:23 +02:00
Mail::to($user->email)->send(new AccountCreated([
'email' => $user->email,
'lastname' => $user->lastname,
'firstname' => $user->firstname,
'password' => $user->password,
]));
2023-03-31 15:24:32 +02:00
return $user;
}
}