我的视图(drivers.blade.php)无法从DriverController索引方法识别变量?我的模型名称是Driver。
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Driver;
class DriverController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$driver = Driver::all();
return view('drivers', compact('drivers'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
Driver::create($request->all());
return back();
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
这应该从数据库中获取所有数据,然后显示它们,我正在尝试创建一个CRUD,但我总是收到此错误“未定义变量:驱动程序(视图:C:\ xampp \ htdocs \ laravel \ resources \ views \ drivers.blade.php)”。
<h2>All Drivers</h2>
<table id="example2" class="table table-responsive" role="grid" aria-describedby="example2_info">
<thead>
<tr>
<th>Fname</th>
<th>Mname</th>
<th>Lname</th>
<th>Phone</th>
<th>Address</th>
<th>Hired</th>
<th>License_no:</th>
</tr>
@foreach($driver as $drive)
<tbody>
<tr>
<td>{{$drive->fname}}</td>
<td>{{$drive->mname}}</td>
<td>{{$drive->lname}}</td>
<td>{{$drive->address}}</td>
<td>{{$drive->hire_date}}</td>
<td>{{$drive->license_no}}</td>
</tr>
</tbody>
@endforeach
</table>
答案 0 :(得分:0)
在索引函数中,变量名称为$ driver,并且您正在传递驱动程序。将其更改为以下内容:
public function index()
{
$driver = Driver::all();
return view('drivers', compact('driver'));
}