简体   繁体   English

Laravel 5.7视图中未定义的变量

[英]Laravel 5.7 Undefined variable in view

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? 它是5.6和5.7之间的兼容性问题还是什么? (please note that I am a noob in Laravel) (请注意,我是Laravel的菜鸟)

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

According to your link I do not see the full code of the controller, but your index method should look like this 根据您的链接,我看不到控制器的完整代码,但是您的index方法应如下所示

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

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

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 Employee.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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM