Mehran
Mehran

Reputation: 282

Pass data to view in Laravel

I am new to Laravel I just want to pass variable to view but every time I have got Undefined variable: companies error this is my Controller:

public function index()
{
    $companies = Company::all();

    return view(
        'companies.index', [
             'companies' => $companies
        ]);
}

and this is my view:

        <ul class="list-group">
            @foreach($companies as $company)
                <li class="list-group-item">{{ $company->name }}</li>
            @endforeach
        </ul>

I use Laravel version 5.5

Upvotes: 2

Views: 355

Answers (1)

bryceandy
bryceandy

Reputation: 176

Make sure in your route file on routes/web.php you have a route to the index function like so:

Route::get('urlToYourView', 'YourController@index');

Upvotes: 1

Related Questions