duandaotuo5542 2018-11-03 10:54
浏览 47
已采纳

我的视图(drivers.blade.php)无法从我的DriverController索引方法中识别变量

My view(drivers.blade.php) cannot identify a variable from DriverController index method? My model name is 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)
{
    //
}
 }

This should get all the data from the database and then display them, i'm trying to create a CRUD but i always get this error "Undefined variable: driver (View: C:\xampp\htdocs\laravelesources\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>
  • 写回答

1条回答 默认 最新

  • du060334 2018-11-03 10:56
    关注

    In your index function your variable name us $driver and you are passing drivers. Change it to following:

    public function index()
    {
        $driver = Driver::all();
        return view('drivers', compact('driver'));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料