0
votes

I have a navigation section built from a loop of my companies model. So the nav looks like this

@foreach ($companies as $company)

{{ link_to("company/{$company->id}/users", $company->name, ['class' => 'btn btn-xs btn-primary']) }}

@endforeach

This grabs all of the company names and id's to build the button links for each company. this works fine on my companies view, but I also want to include this in the main layout navigation.

What is the best why to do this? I was thinking to add a function to the base controller but not sure how or what view to return?

1

1 Answers

0
votes

Create a file, you can name something like views/partials/companies.blade.php and add your foreach in it:

@foreach ($companies as $company)

    {{ link_to("company/{$company->id}/users", $company->name, ['class' => 'btn btn-xs btn-primary']) }}

@endforeach

Then, everywhere you need it, you just have to tell Blade to include that partial:

@include('partials.companies')

Having your data globally available for this partial would require a View Composer:

View::composer(array('your.first.view','your.second.view'), function($view)
{
    $view->with('companies', Company::all());
});

You can create a file to store your composers, something like app/composers.php and load it in your app/start/global.php:

require app_path().'/composers.php';