Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mkellyxp's avatar

How to get dynamic data into a Layout file?

So i'm working on a pretty basic application where the app has a typical shell with a side bar. I'm using a layout for the overall page, and then the individual page views that extend the layout.

No problem.. works great!

However, there is a dropdown in the sidebar with dynamic options from the database. I'm currently grabbing these options in the individual page controller, and then passing them into the view... which then passes it into the layout.

But this means I'd need to do this for every page controller. Is this the way to do this, or is there a perferred way that I don't have to repeat myself on every page controller. Like is there a layout controller?

Just thinking about best practices here. Thanks!

0 likes
4 replies
mkellyxp's avatar

Thanks.. on the surface, this seems like what I need.. but for some reason the docs are crazy confusing to me. Something just isnt' clicking and this seems way more complicated than it needs to be?

Is there a class in Laracasts or a recommended tutorial to step through setting this up the first time

MarianoMoreyra's avatar

Hi @mkellyxp

As @snapey suggested, View Composers would be a great solution in this case.

I don't have a tutorial to suggest here and without more details I can only try to give you a basic step by step guide to adapt to your use case.

You can start by creating the View Composer class. This class will provide the data for the sidebar's dropdown. As I don't know what information will you be showing there, lets just use options from an Option model as an example:

app\Http\View\Composers\LayoutComposer.php

<?php

namespace App\Http\View\Composers;

use App\Option;
use Illuminate\View\View;

class LayoutComposer
{
    public function compose(View $view)
    {
        $view->with('options', Option::all());
    }
}

As you can see, for this example I'm just using the folder View\Composers inside app\Http as suggested on docs.

Then, just to simplify and because it might be the only one you'll use, you could register the view composer within the AppServiceProvider just like this:

app\Providers\AppServiceProvider.php

<?php

namespace App\Providers;

use App\Http\View\Composers\LayoutComposer;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function boot(Dispatcher $events)
    {
        View::composer('layouts.master', LayoutComposer::class);
    }
}

Finally, you can access the $options variable (that you've passed from the compose method) inside your layout view as you would do with variables passed from a Controller to render the options at your side bar.

Note that in my example, I have a master.blade.php view inside a layouts folder, so you should change the first argument from 'layouts.master' to point to your layout view.

Hope this helps!

Please or to participate in this conversation.