问题描述
我在上次讨论中发帖,可能不清楚我的问题.如果我创建了 2 个相同的讨论,我深表歉意现在我将附上所有信息,包括所有代码,希望能尽快修复.
还有SiswaController.php,希望能帮到大家支持我的问题.
命名空间 App\Http\Controllers;使用 Illuminate\Http\Request;使用 App\Siswa;使用会话;使用 App\Exports\SiswaExport;使用 App\Imports\SiswaImport;使用 Maatwebsite\Excel\Facades\Excel;使用 App\Http\Controllers\Controller;类 SiswaController 扩展控制器{公共函数索引(){$siswa = Siswa::all();返回视图('siswa',['siswa'=>$siswa]);}公共函数export_excel(){返回 Excel::download(new SiswaExport, 'siswa.xlsx');}公共函数 import_excel(Request $request){//验证$this->validate($request, ['文件' =>'必需| mimes:csv,xls,xlsx']);//menangkap文件excel$file = $request->file('file');//membuat nama文件unik$nama_file = rand().$file->getClientOriginalName();//上传ke文件夹file_siswa di dalam文件夹public$file->move('file_siswa',$nama_file);//导入数据Excel::import(new SiswaImport, public_path('/file_siswa/'.$nama_file));//通知 dengan 会话Session::flash('sukses','Data Siswa Berhasil Diimport!');//阿里坎哈拉曼肯巴利返回重定向('/siswa');}}
有siswa.blade.php
<头><tr><th>No</th><th>Nama</th><th>NIS</th><th>Alamat</th></tr></thead>@php $i=1 @endphp@foreach($siswa 作为 $s)<tr><td>{{ $i++ }}</td><td>{{$s->nama}}</td><td>{{$s->nis}}</td><td>{{$s->alamat}}</td></tr>@endforeach</tbody>有web.php
解决方案 在你的路由文件 web.php
Route::get('/', function () {返回视图('siswa');});
这里 $siswa 没有被传递,所以它产生了未定义的变量 $siswa.
我想你想把它导入SiswaController@index
I was posting on last discussion, may be is no clear my problem.I'm apologize if I was created 2 same discussionAnd now I will attach all information including all code, I hope it can be fixed soon.
and there are SiswaController.php, I hope can help all of you for support my problem.
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');
}
}
There is 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>
There are 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');
解决方案 In your route file web.php
Route::get('/', function () {
return view('siswa');
});
here $siswa is not passed, so it is resulting undefined variable $siswa.
I think you want to direct it into SiswaController@index
这篇关于ErrorException 未定义变量 $siswa 在哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
06-28 19:30