Php Laravel 5.7视图中的未定义变量
我按照指南()进行操作,在“如何在Laravel 5.6中创建列表”一点上,我得到了一个错误: 错误异常(E_错误) 未定义变量:employees(视图:C:\xampp\htdocs\crud\resources\views\pages\index.blade.php) 以前的例外情况 *未定义变量:雇员(0) 在代码窗口中,错误为:Php Laravel 5.7视图中的未定义变量,php,laravel,Php,Laravel,我按照指南()进行操作,在“如何在Laravel 5.6中创建列表”一点上,我得到了一个错误: 错误异常(E_错误) 未定义变量:employees(视图:C:\xampp\htdocs\crud\resources\views\pages\index.blade.php) 以前的例外情况 *未定义变量:雇员(0) 在代码窗口中,错误为: <?php $__currentLoopData = $employees; $__env->addLoop($__currentLoopDat
<?php
$__currentLoopData = $employees;
$__env->addLoop($__currentLoopData);
foreach($__currentLoopData as $key => $emp):
$__env->incrementLoopIndices();
$loop = $__env->getLastLoop(); ?>
这是5.6和5.7之间的兼容性问题还是什么?(请注意,我是拉雷维尔的一名noob)指南非常纤细,您需要做些什么才能使索引正常工作:
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
该指南非常精简,您需要做些什么才能使索引正常工作:
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
根据您的链接,我看不到控制器的完整代码,但您的索引方法应如下所示
public function index()
{
$employees = Employee::all();
// Pass data to view
return view('employee.index', ['employees' => $employees]);
}
根据您的链接,我看不到控制器的完整代码,但是您的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>
@foreach($key=>$emp的员工)
{{$emp->id}
@endforeach
到目前为止,我的代码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>
@foreach($key=>$emp的员工)
{{$emp->id}
@endforeach
能否显示控制器代码?错误很简单,$employees
缺失。确保您已将其传递到视图中。如果没有输入错误,'employee'=>$employees
是否将employees变量传递给视图?能否显示控制器代码?错误非常简单,$employees
缺失。确保您已将其传递到视图中。如果您没有输入错误,'employee'=>$employees
是否将employees变量传递给视图?您应该返回页面。index
而不是employees.index
您是正确的,我将其更改为页面。index
,但错误保持不变。我还尝试了returnview('pages.index')->with('employees',$employees)代码>,但未更改。请尝试清除缓存的视图。还有,你的路线是什么样的?您在浏览器中访问的url是什么?当我删除
标记之间的代码时,页面加载正常。web.php有两行代码Route::get('/','PagesController@index'); 路线::资源('employee','EmployeeController')
。您在浏览器中点击的url是什么?您应该返回页面。index
而不是员工。index
您是正确的,我将其更改为页面。index
但错误保持不变。我还尝试了returnview('pages.index')->with('employees',$employees)代码>,但未更改。请尝试清除缓存的视图。还有,你的路线是什么样的?您在浏览器中访问的url是什么?当我删除
标记之间的代码时,页面加载正常。web.php有两行代码Route::get('/','PagesController@index'); 路线::资源('employee','EmployeeController')代码>。您在浏览器中点击的url是什么?