Rizwan Ejaz
Rizwan Ejaz

Reputation: 296

laravel 5: View Composer not working

I am trying to pass data that would be displayed to the navigation for all pages. I have added a view composer to App Service Provider boot method. Here is how it looks:

public function boot()
    {
        View::composer('_nav', function($view)
        {
            $view->with('catList', Category::all());
        });
}

In _nav partial I am trying to count the value of $catList but laravel gives following error:

Undefined variable: catList (View: /home/ubuntu/workspace/resources/views/_nav.blade.php) 

Upvotes: 1

Views: 2200

Answers (2)

M.A.Naseer
M.A.Naseer

Reputation: 343

i had a similar issue, following worked for me,

add following use statement at the top:

use View;

Then

public function boot() {    
   View::composer('partials.sidebar', function($view)
    {
        $title = "Page Title";
        $view->with('title', $title);
    });
}

If you still have issues, try to add debug statement and see if your

Category::all()

call is returning you data.

Upvotes: 0

Margus Pala
Margus Pala

Reputation: 8663

In Laravel 5 the view composers are handled bit differently compared to Laravel 4. Try something like below.

public function boot()
{
    $this->app['view']->composer('_nav',function($view){
        $view->catList =  Category::all();
    });
}

Upvotes: 1

Related Questions