未定义变量:Laravel 5.7中的student(视图:D:\exam\curd\resources\views\home.blade.php)

未定义变量:Laravel 5.7中的student(视图:D:\exam\curd\resources\views\home.blade.php),php,laravel,Php,Laravel,使用laravel 5.7,需要在主页home.blade.php中显示学生表数据 <table class="table"> <thered> <tr> <td>Id</td> <td>Name</td> <td>Address</td> <td>Telephone</td>

使用laravel 5.7,需要在主页home.blade.php中显示学生表数据

<table class="table">
    <thered>
     <tr>
         <td>Id</td>
         <td>Name</td>
         <td>Address</td>
         <td>Telephone</td>
         <td>Actions</td>
     </tr>
    </thead>

    <tbody>
      @foreach ($students as $student)
      <tr>
        <td>{{$student->id}}</td>
        <td>{{$student->name}}</td>
        <td>{{$student->address}}</td>
        <td>{{$student->telephone}}</td>
        <td><a class="button is-outlined" href="">Edit</a></td>
        <td><a class="button is-outlined" href="">Delete</a></td>
      </tr>
      @endforeach
    </tbody>
</table>
web.php

Route::get('StudentController@index');
但是得到了这样的错误信息

未定义变量:students(视图:D:\exam\curd\resources\views\home.blade.php)


如何解决此问题?

您需要将学员传递到视图,因此在控制器中添加以下内容:

return view('home', compact('students'));
你的路线是:

Route::get('/home', 'StudentController@index'); // if this is your root route

您需要在视图中传递
$students
变量。您可以使用
compact()
函数或其他许多方法来实现这一点

public function index()
{
    $students = Student::all();
    return view('home', compact('students'));
}

在控制器的方法中尝试
dd('hit')
,但出现了相同的错误。确保它命中了
index()
method。没有,但出现了相同的错误here@banda除非您没有通过该控制器方法加载页面,否则这是不可能的。检查我对路线的编辑。也使用了上面的路线,但这里没有成功,这里有相同的错误。您可以尝试在控制器中转储一些内容,以确保您至少进入了那里吗?在索引函数中尝试
dd('test')如果这是在您的浏览器中打印的。fine@nakov现在工作正常
public function index()
{
    $students = Student::all();
    return view('home', compact('students'));
}