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/0151452fac1c56e788e2e0265737ec70 to your computer and use it in GitHub Desktop.
Save cursosdesarrolloweb/0151452fac1c56e788e2e0265737ec70 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Controllers;
use App\Imports\UsersImport;
use App\Models\User;
use Illuminate\Http\RedirectResponse;
use Maatwebsite\Excel\Facades\Excel;
class UserController extends Controller
{
/**
* @return RedirectResponse
* @throws ValidationException
*/
public function import(): RedirectResponse
{
$this->validate(request(), [
'file' => 'required|mimetypes::'.
'application/vnd.ms-office,'.
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,'.
'application/vnd.ms-excel',
]);
try {
set_time_limit(0);
DB::beginTransaction();
Excel::import(new UsersImport(), request("file"));
DB::commit();
return back()
->with('notification', ['type' => 'success', 'title' => 'Usuarios importados']);
} catch (\Exception $exception) {
DB::rollBack();
return back()
->with('notification', ['type' => 'danger', 'title' => 'Error importando usuarios']);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment