Ejemplo 1: laravel excel
//Install this package https://docs.laravel-excel.com/3.1/getting-started/installation.html
php artisan make:import UsersImport --model=User
<?php
namespace AppImports;
use AppUser;
use IlluminateSupportFacadesHash;
use MaatwebsiteExcelConcernsImportable;
use MaatwebsiteExcelConcernsToModel;
use MaatwebsiteExcelConcernsWithProgressBar;
class UsersImport implements ToModel, WithProgressBar
{
use Importable;
public function model(array $row)
{
return new User([
'name' => $row[0],
'email' => $row[1],
'password' => Hash::make($row[2]),
]);
}
}
//In your console command, you'd use it as follows:
<?php
namespace AppConsoleCommands;
use AppImportsUsersImport;
use IlluminateConsoleCommand;
class ImportExcel extends Command
{
protected $signature = 'import:excel';
protected $description = 'Laravel Excel importer';
public function handle()
{
$this->output->title('Starting import');
(new UsersImport)->withOutput($this->output)->import('users.xlsx');
$this->output->success('Import successful');
}
}
// finally run this command
php artisan import:excel
Ejemplo 2: laravel excel
namespace AppImports;
use AppUser;
use MaatwebsiteExcelConcernsToModel;
class UsersImport implements ToModel
{
}
Ejemplo 3: laravel excel
namespace AppImports;
use AppUser;
use MaatwebsiteExcelConcernsToModel;
class UsersImport implements ToModel
{
public function model(array $row)
{
return new User([
'email' => $row[1],
])
}
}
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)