Laravel View Composers with Layouts

1 day ago 3
ARTICLE AD BOX

Description

I have recently updated my code to put layouts in resources/views/layouts instead of resources/views/components/layouts. This means I have also updated all pages that use these components to use Blade's layout namespace.

Example

Rendering a layout in resources/views/layouts/app/sidebar.blade.php used to be done by...

<x-layouts.app.sidebar> ... </x-layouts.app.sidebar>

Now after the migration...

<x-layouts::app.sidebar> ... </x-layouts::app.sidebar>

The Problem

I have a View composer setup for my sidebar to populate the sidebar with data. Before the migration the composer was called in AppServiceProvider like so...

Facades\View::composer('components.layouts.app.sidebar', UserFriendsComposer::class);

so after the migration I updated this code to...

Facades\View::composer('layouts.app.sidebar', UserFriendsComposer::class);

But now the variables are not getting passed to the layout through the composer and I am not sure what I am missing here. What is puzzling is this code worked fine before I moved my layouts out of the components directory.

Thanks

Read Entire Article