I am trying to push an array to the blade template in laravel 4 Controller name: AdminController
public function listCompanies()
{
$companyObj = new company();
$companies = $companyObj->getCompanies();
$this->layout->content = View::make('admin/main',$companies);
}
Template name: main.blade.php
<div class="container">
@yield('content')
@foreach ($companies as $company)
<p>This is company {{ $company['name'] }}</p>
@endforeach
</div>
The error I am getting is: Undefined variable: companies (View: C:\\wamp\\www\\larvel-project\\laravel\\app\\views\\admin\\main.blade.php)
The view is expecting to receive an associative array which includes the index 'companies' and will turn that into $companies.
Any of the following should work for you:
$this->layout->content = View::make('admin/main', ['companies' => $companies]);
$this->layout->content = View::make('admin/main', compact('companies'));
$this->layout->content = View::make('admin/main')->with('companies', $companies);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.