1

I'm trying to include a view on all of my pages (code is below), so I've included it within my layout template (so it renders on everything). Unfortunately, when I try and run any page - this error occurs:

Undefined variable: sites (View: /Users/Documents/audit/resources/views/layouts/check.blade.php)

View (check.blade.php):

@if (count($sites) == 0)
 // Removed as it is irrelevant.
@endif

Controller:

public function siteCheck()
{
  return View::make('layouts.check')
                  ->with('sites', Site::where('user_id', Auth::id())
                  ->get());
}

Where I try and include the view (which shows the error):

@if(!Auth::guest())
  @include('layouts.check')
@endif

N.B. I haven't added any code relating to the layouts.check page within the routes.

Many thanks for your help.

2 Answers 2

1

Problem is that when You include file layouts.check method siteCheck() isn't launched (and therefore varaible $sites don't exist).

You have couple of options:

  1. add a variable when incuding file

@include('layouts.check', ['sites' => $sites]) (Still You will need to pass $sites from the contoller tho the master view.)

  1. add view composer which add this variable every time the view will be included

see: https://laravel.com/docs/5.2/views#view-composers

In Your case it would look like this:

public function boot()
{
    view()->composer('layouts.check', function ($view) {

        $view->with('sites', Site::where('user_id', Auth::id())
              ->get());
    });
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try to this

@if(isset($sites)&&count($sites) == 0)
 // Removed as it is irrelevant.
@endif

NOTE:

whene you use @include('layouts.check') you not set $sites

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.