我是Laravel的新手,我只想将变量传递给视图,但每次我都会出现Undefined variable: companies
错误这是我的控制器:
public function index()
{
$companies = Company::all();
return view(
'companies.index', [
'companies' => $companies
]);
}
这是我的观点:
<ul class="list-group">
@foreach($companies as $company)
<li class="list-group-item">{{ $company->name }}</li>
@endforeach
</ul>
我使用Laravel 5.5版
确保在routes/web.php
上的路由文件中有一个到索引函数的路由,如下所示:
Route::get('urlToYourView', 'YourController@index');