Level 2
I solve this problem by Myself following the site tutorial : http://itsolutionstuff.com/post/laravel-53-import-export-csv-and-excel-file-into-databaseexample.html
1 like
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
DemoController.php
<?php
use App\User;
class DemoController extends Controller
{
public function importExport()
{
return view('importExport');
}
public function downloadExcel($type)
{
$data = users::get()->toArray();
return Excel::create('users', function($excel) use ($data) {
$excel->sheet('mySheet', function($sheet) use ($data)
{
$sheet->fromArray($data);
});
})->download($type);
}
public function importExcel()
{
if(Input::hasFile('import_file')){
$path = User::file('import_file')->getRealPath();
$data = Excel::load($path, function($reader) {
})->get();
}
return back();
}
}
importExport.blade.php
<html lang="en">
<head>
<title>Import - Export Laravel 5</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" >
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">Import - Export in Excel and CSV Laravel 5</a>
</div>
</div>
</nav>
<div class="container">
<a href="{{ URL::to('downloadExcel/xls') }}"><button class="btn btn-success">Download Excel xls</button></a>
<a href="{{ URL::to('downloadExcel/xlsx') }}"><button class="btn btn-success">Download Excel xlsx</button></a>
<a href="{{ URL::to('downloadExcel/csv') }}"><button class="btn btn-success">Download CSV</button></a>
<form style="border: 4px solid #a1a1a1;margin-top: 15px;padding: 10px;" action="{{ URL::to('importExcel') }}" class="form-horizontal" method="post" enctype="multipart/form-data">
<input type="file" name="import_file" />
<button class="btn btn-primary">Import File</button>
</form>
</div>
</body>
</html>
web.php
Route::get('importExport', 'DemoController@importExport');
Route::get('downloadExcel/{type}', 'DemoController@downloadExcel');
Route::post('importExcel', 'DemoController@importExcel');
I am getting error as a Whoops, looks like something went wrong.
1/1 FatalErrorException in DemoController.php line 5: Class 'Controller' not found
I solve this problem by Myself following the site tutorial : http://itsolutionstuff.com/post/laravel-53-import-export-csv-and-excel-file-into-databaseexample.html
Please or to participate in this conversation.