【问题标题】:where is an ErrorException Undefined variable $siswaErrorException 未定义变量 $siswa 在哪里
【发布时间】: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


【解决方案1】:

在你的路由文件 web.php 中

Route::get('/', function () {
    return view('siswa');
});

这里 $siswa 没有被传递,所以结果是未定义的变量 $siswa。

我想你想把它指向SiswaController@index

【讨论】:

  • 我正在尝试这样 ``` Route::get('/', function () { return view('SiswaController@index'); }); Route::get('/siswa', 'SiswaController@index');路线::get('/siswa/export_excel', 'SiswaController@export_excel'); Route::post('/siswa/import_excel', 'SiswaController@import_excel'); ``` 但是有错误信息“InvalidArgumentException View [SiswaController@index] not found.”
  • Route::get('', 'SiswaController@index' ); 应该足够了。
  • 我正在尝试并遇到错误“Illuminate\Contracts\Container\BindingResolutionException 目标类 [SiswaController] 不存在。”
  • @LucasFutami 所以你的命名空间不正确,告诉我你的控制器的命名空间。您的路由文件现在无法识别它。
  • @LucasFutami 您也可以在使用之前导入您的控制器。只需在 web.php 顶部使用类似 use App\Http\Controllers\SiswaController; 的导入。
【解决方案2】:

我认为您忘记在 SiswaController.php 中包含 Siswa 模型。

添加该模型路径并尝试。

use App\Models\Siswa;

我相信错误会得到解决。

【讨论】:

  • 这是评论,不是答案。
  • @GertB。没关系,但这是解决方案。
  • 我试过了,还是一样的错误“ErrorException Undefined variable $siswa (View: D:\xampp\htdocs\report_sales\resources\views\siswa.blade.php)localhost:8080/report_sales/public”跨度>
  • 如果缺少 use 语句,您会收到另一个错误。他的代码中需要它,但不会导致错误
  • @GertB。我给代码怎么写?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-05
  • 2018-01-26
  • 2020-09-24
  • 2018-11-29
  • 2020-04-16
相关资源
最近更新 更多