Skip to content

Instantly share code, notes, and snippets.

@cursosdesarrolloweb
Last active Jun 12, 2021
Embed
What would you like to do?
<?php
namespace App\Imports;
use App\Models\User;
use Illuminate\Support\Str;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithStartRow;
class UsersImport implements ToModel, WithStartRow
{
/**
* EXCEL COLUMNS FORMAT
* NAME | EMAIL
* @param array $row
*/
public function model(array $row)
{
$password = Str::random(8);
$userExists = User::whereEmail($row[1])->first();
if ( ! $userExists) {
$user = User::create(
[
'name' => $row[0],
'email' => $row[1],
'password' => bcrypt($password),
]
);
return $user;
}
}
public function startRow(): int {
return 2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment