[英]Laravel 5.7 Undefined variable in view
我遵循指南( https://www.phpflow.com/php/laravel-5-6-crud-operation-using-resource-controller/ ),在“如何在Laravel 5.6中創建清單”這一點上,我得到了錯誤::
ErrorException(E_ERROR)未定義的變量:雇員(視圖:C:\\ xampp \\ htdocs \\ crud \\ resources \\ views \\ pages \\ index.blade.php)
以前的例外*未定義的變量:員工(0)
在代碼窗口中的錯誤是:
<?php
$__currentLoopData = $employees;
$__env->addLoop($__currentLoopData);
foreach($__currentLoopData as $key => $emp):
$__env->incrementLoopIndices();
$loop = $__env->getLastLoop(); ?>
它是5.6和5.7之間的兼容性問題還是什么? (請注意,我是Laravel的菜鳥)
該指南非常苗條,您需要做些什么才能使索引正常工作:
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
}
如果您的資源定義是:
Route::resource('employee', 'EmployeeController');
訪問此文件的路徑(URL)為: /employee
根據您的鏈接,我看不到控制器的完整代碼,但是您的index
方法應如下所示
public function index()
{
$employees = Employee::all();
// Pass data to view
return view('employee.index', ['employees' => $employees]);
}
該錯誤仍然存在,到目前為止是我的代碼的一部分,EmployeeController.php:
public function index()
{
$employees = Employee::all();
return view('employee.index', ['employees' => $employees]);
}
Employee.php
class Employee extends Model
{
// Table name
protected $table = 'crud';
// Primary key
public $primaryKey = 'id';
}
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.