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

chrisgrim's avatar

How To Test Why My View-Composer isn't working

I believe I have setup my View-Composer correctly as per the instructions on https://laravel.com/docs/5.8/views#view-composers

I created a new ComposerServiceProvider with php artisan make:provider ComposerServiceProvider. In that file I have

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\View;

class ComposerServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        View::composer(
            'layouts.nav', 'App\Http\View\Composers\CategoryComposer'
        );
    }
}

Then I registered it in my config/app.php

'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
 
        App\Providers\ComposerServiceProvider::class,

    ],

Then in the folder App\Http\View\Composers\ I created a file CategoryComposer.php and in that I have

<?php

namespace App\Http\View\Composers;

use Illuminate\View\View;
use App\Category;

class CategoryComposer
{
    /**
     * Bind data to the view.
     *
     * @param  View  $view
     * @return void
     */
    public function compose(View $view)
    {
        $allcategories = Category::all();
        $view->with('allcategories', $allcategories);
    }
}

Finally in my views/layouts/nav.blade.php file I have

                    @foreach($allcategories as $category)
                        <li><a href="/bestof/{{$category->slug}}" class="text-white">{{$category->name}}</a></li>
                    @endforeach

Yet when I try to look at the page I am getting the error "Undefined variable: allcategories

Am I missing something? How do I even test for what I am missing?

0 likes
7 replies
Ty's avatar

First I would add the view facade before the service provider in ComposerServiceProvider

chrisgrim's avatar

I tried switching them with no luck. Here is the latest ComposerServiceProvider

<?php

namespace App\Providers;

use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;

class ComposerServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        View::composer(
            'layouts.nav', 'App\Http\View\Composers\CategoryComposer'
        );
    }
}
Snapey's avatar

dont forget you can dd() at any of these steps to see if all are being called

chrisgrim's avatar

@snapey Good Call! In my CategoryComposer I added

    public function compose(View $view)
    {
        $allcategories = Category::all();
        dd($allcategories);
        $view->with('allcategories', $allcategories);
    }

and nothing so I am guessing it isn't being called. In my ComposerServiceProvider how would I add dd()? I have tried

    public function boot()
    {
        dd('test');
        View::composer(
            'layouts.nav', 'App\Http\View\Composers\CategoryComposer'
        );
    }

but nothing happens. So does that mean I am doing something wrong with how I am loading it in my providers list inside app.php? The ComposerServiceProvider.php is inside the folder App\Providers\ComposerServiceProvider::class.

Snapey's avatar
Snapey
Best Answer
Level 122

is your config cached? run php artisan config:clear

4 likes
tykus's avatar

I don't believe it should matter, but your App\Providers\ComposerServiceProvider class appears to be referenced before all of the Laravel Framework service providers; I will typically add mine in the section under the comment:

        /*
         * Application Service Providers...
         */

Since you are registering the view composer in the boot method, this should not matter AFAIK, but worth a shot...

Please or to participate in this conversation.