Skip to content

Instantly share code, notes, and snippets.

@cursosdesarrolloweb
Last active June 12, 2021 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cursosdesarrolloweb/b783594a9385753d6f165dce390cefe4 to your computer and use it in GitHub Desktop.
Save cursosdesarrolloweb/b783594a9385753d6f165dce390cefe4 to your computer and use it in GitHub Desktop.
<?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