【发布时间】:2021-11-05 18:43:41
【问题描述】:
我在上次讨论中发帖,可能不清楚我的问题。 如果我创建了 2 个相同的讨论,我很抱歉 现在我将附上所有信息,包括所有代码,我希望它可以尽快修复。
还有SiswaController.php,希望能帮助大家支持我的问题。
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Siswa;
use Session;
use App\Exports\SiswaExport;
use App\Imports\SiswaImport;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Controller;
class SiswaController extends Controller
{
public function index()
{
$siswa = Siswa::all();
return view('siswa',['siswa'=>$siswa]);
}
public function export_excel()
{
return Excel::download(new SiswaExport, 'siswa.xlsx');
}
public function import_excel(Request $request)
{
// validasi
$this->validate($request, [
'file' => 'required|mimes:csv,xls,xlsx'
]);
// menangkap file excel
$file = $request->file('file');
// membuat nama file unik
$nama_file = rand().$file->getClientOriginalName();
// upload ke folder file_siswa di dalam folder public
$file->move('file_siswa',$nama_file);
// import data
Excel::import(new SiswaImport, public_path('/file_siswa/'.$nama_file));
// notifikasi dengan session
Session::flash('sukses','Data Siswa Berhasil Diimport!');
// alihkan halaman kembali
return redirect('/siswa');
}
}
有siswa.blade.php
<a href="/siswa/export_excel" class="btn btn-success my-3" target="_blank">EXPORT EXCEL</a>
<table class='table table-bordered'>
<thead>
<tr>
<th>No</th>
<th>Nama</th>
<th>NIS</th>
<th>Alamat</th>
</tr>
</thead>
<tbody>
@php $i=1 @endphp
@foreach($siswa as $s)
<tr>
<td>{{ $i++ }}</td>
<td>{{$s->nama}}</td>
<td>{{$s->nis}}</td>
<td>{{$s->alamat}}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
有web.php
<?php
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', 'SiswaController@index');
Route::get('/siswa', 'SiswaController@index');
Route::get('/siswa/export_excel', 'SiswaController@export_excel');
Route::post('/siswa/import_excel', 'SiswaController@import_excel');
【问题讨论】:
-
@RushikeshGanesh 说的基本一样,不是每个人都喜欢用
compact() -
@GertB。你能帮帮我吗?
-
我是从这个网站malasngoding.com/import-excel-laravel关注的,但是尝试时出现问题
-
你在什么操作上得到错误,索引函数看起来很好。
-
不知道,有错误> ErrorException Undefined variable $siswa (View: D:\xampp\htdocs\report_sales\resources\views\siswa.blade.php)
标签: php excel laravel import undefined