-
-
Save cursosdesarrolloweb/0151452fac1c56e788e2e0265737ec70 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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