Support the ongoing development of Laravel.io →
Eloquent Views Blade
Last updated 2 years ago.
0

you can include blade partials into other views via @include('folder.filename')

then you need a view composer that listens to folder.filename and injects the necessary variable into that view

source: http://laravel.com/docs/templates (including sub views)

source: http://laravel.com/docs/responses#view-composers

Last updated 2 years ago.
0

Use a single view as template for all views or extends BaseController, and use

View::share($arrayOfDataAvailableToAllViews);

es.

class BaseController2 extends BaseController{
    public function __construct(){
        $data = array('foo'=>'Foo', 'bar'=>'Bar');
        View::share($data);
    }
    
}

Then if your controllers extends BaseController2, all views will share $data array.

If $data will be a db query result cache it

    $data = MyModel::remember(10)->get();
Last updated 2 years ago.
0

longilineo said:

class BaseController2 extends BaseController{ $data = array('foo'=>'Foo', 'bar'=>'Bar'); View::share($data); }

Mmmh, that View::share... shouldn't it be inside a function? I get the following error:

 syntax error, unexpected '$data' (T_VARIABLE), expecting function (T_FUNCTION) 
Last updated 2 years ago.
0

yes, there was an error, take a look at updated reply

Last updated 2 years ago.
0

Then I don't get the error but the variable is not recognized:

 Undefined variable: data (View: /Users/Maggie/Sites/hotesses/laravel/app/views/master.blade.php) 
Last updated 2 years ago.
0

In your views you should have $foo and $bar variables available.

Last updated 2 years ago.
0

longilineo said:

In your views you should have $foo and $bar variables available.

Duh, of course, my bad, thanks a lot

Last updated 2 years ago.
0

Ok, it works with the array, but how does it work with the db query? I have:

$hostesses = Hostess::remember(10)->get();
View::share($hostesses);

And upon using var_dump($hostesses) I get an Illegal offset type error

Last updated 2 years ago.
0

Have you checked out view composers ?

http://laravel.com/docs/responses#view-composers

You can make it work for multiple views. Possibly all.

Last updated 2 years ago.
0

I get an Illegal offset type error

Where? In views? Or in BaseController2?

In order to retrieve $hostesses variable in views, in BaseController2 you should have:

$hostesses = Hostess::remember(10)->get();
View::share(array("hostesses"=>$hostesses)); 
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.