Laravel 5.7 中未定义的变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53619166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:11:35  来源:igfitidea点击:

Laravel 5.7 Undefined variable in view

phplaravel

提问by Rain

I follow the guide ( https://www.phpflow.com/php/laravel-5-6-crud-operation-using-resource-controller/) and at point "How To Create Listing in Laravel 5.6" I get the error::

我按照指南(https://www.phpflow.com/php/laravel-5-6-crud-operation-using-resource-controller/)在“如何在 Laravel 5.6 中创建列表”时出现错误::

ErrorException (E_ERROR) Undefined variable: employees (View: C:\xampp\htdocs\crud\resources\views\pages\index.blade.php)

ErrorException (E_ERROR) 未定义变量:雇员(视图:C:\xampp\htdocs\crud\resources\views\pages\index.blade.php)

Previous exceptions * Undefined variable: employees (0)

以前的异常 * 未定义变量:雇员 (0)

And in code window the error is:

在代码窗口中,错误是:

<?php 
$__currentLoopData = $employees;
$__env->addLoop($__currentLoopData);
foreach($__currentLoopData as $key => $emp): 
    $__env->incrementLoopIndices();
    $loop = $__env->getLastLoop(); ?>

Is it a compatibility issue between 5.6 and 5.7 or what? (please note that I am a noob in Laravel)

这是 5.6 和 5.7 之间的兼容性问题还是什么?(请注意,我是 Laravel 的菜鸟)

回答by adam

The guide is pretty slim, what you need to do in order to get you index working:

该指南非常精简,您需要做什么才能使索引正常工作:

namespace App\Http\Controllers;

use App\Employee;
use Illuminate\Http\Request;

class EmployeeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        return view('pages.index', ['employees' => Employee::all()]);
    }

    // ... The rest of the controller method below
}

If your resource definition is:

如果您的资源定义是:

Route::resource('employee', 'EmployeeController');

The Path (URL)to access this will be: /employee

访问它的路径(URL)将是:/employee

回答by aleksejjj

According to your link I do not see the full code of the controller, but your indexmethod should look like this

根据你的链接,我没有看到控制器的完整代码,但你的index方法应该是这样的

public function index()
{
    $employees = Employee::all();

    // Pass data to view
    return view('employee.index', ['employees' => $employees]);
}

回答by Rain

The error remains, part of my code so far, EmployeeController.php:

错误仍然存​​在,到目前为止我的代码的一部分,EmployeeController.php:

public function index()
{
$employees = Employee::all();
return view('employee.index', ['employees' => $employees]);
}

Employee.php

员工.php

class Employee extends Model
{
// Table name
protected $table = 'crud';
// Primary key
public $primaryKey = 'id';
}

index.blade.php

index.blade.php

<tbody>
    @foreach($employees as $key => $emp)
    <tr>
        <td>{{ $emp->id }}</td>
    </tr>
    @endforeach
</tbody>