使用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>
StudentController.php
public function index()
{
$students = Student::all();
return view('home');
}
web.php
Route::get('StudentController@index');
但是却收到了这样的错误消息,
未定义变量:学生(视图:D:\ exam \ curd \ resources \ views \ home.blade.php)
如何解决此问题?
答案 0 :(得分:1)
您需要将学生传递到视图,因此请在控制器中添加它:
return view('home', compact('students'));
您的路线:
Route::get('/home', 'StudentController@index'); // if this is your root route
答案 1 :(得分:0)
您需要在视图中传递$students
变量。您可以使用compact()
函数来完成此操作,或者有许多其他方法。
public function index()
{
$students = Student::all();
return view('home', compact('students'));
}