【发布时间】:2021-05-30 08:46:54
【问题描述】:
我正在使用 Laravel 8 开发我的项目。
现在我想显示数据库中的一些数据,所以我在控制器中编写了代码:
use App\Models\Question;
public function index()
{
questions = Question::all();
return view('home', compact('questions'));
}
然后在视图home.blade.php,我添加了这个:
@foreach($questions as $question)
<tr>
<td style="text-align:right;">
<h3 class="h5 mb-0"><a href="#0" class="text-uppercase">{{ $question->title }}</a></h3>
</td>
</tr>
@endforeach
但现在我得到了这个错误:
错误异常 未定义变量:问题(查看:home.blade.php)
那么这里出了什么问题?我该如何解决这个问题?
如果您能分享您对此的想法和建议,我将不胜感激...
这里也是web.php路线,如果你想看看:
Route::get('/home', [HomeController::class, 'index'])->name('home');
提前致谢。
【问题讨论】:
-
将
questions更改为$questions