1

I used a service provider to create the archives and tags on a side bar for every view. I am following the Laracasts video series and I could swear I have done everything that the instructor has done. However I get an undefined variable error. I cannot figure out why for the life of me. Have any of you had this problem? Do you know what I am doing wrong?

The error message is as follows (I get both because I tried to comment out the archives to see if tags would work):

"Undefined variable: archives (View: C:\Users\Andrew\Homestead\WanderlustCentre\resources\views\layouts\sidebar.blade.php) (View: C:\Users\Andrew\Homestead\WanderlustCentre\resources\views\layouts\sidebar.blade.php) (View: C:\Users\Andrew\Homestead\WanderlustCentre\resources\views\layouts\sidebar.blade.php)"

"Undefined variable: tags (View: C:\Users\Andrew\Homestead\WanderlustCentre\resources\views\layouts\sidebar.blade.php) (View: C:\Users\Andrew\Homestead\WanderlustCentre\resources\views\layouts\sidebar.blade.php) (View: C:\Users\Andrew\Homestead\WanderlustCentre\resources\views\layouts\sidebar.blade.php)"

My files are as follows:

AppServiceProvider.php

<?php

namespace App\Providers;

use App\Post;
use App\Tag;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{

    protected $defer = true;

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        view()->composer('layouts.sidebar', function ($view) {

            $view->with('archives', Post::archives());
            $view->with('tags', Tag::has('posts')->pluck('name'));

        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

config/app.config

'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        Illuminate\Auth\AuthServiceProvider::class,
        Illuminate\Broadcasting\BroadcastServiceProvider::class,
        Illuminate\Bus\BusServiceProvider::class,
        Illuminate\Cache\CacheServiceProvider::class,
        Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
        Illuminate\Cookie\CookieServiceProvider::class,
        Illuminate\Database\DatabaseServiceProvider::class,
        Illuminate\Encryption\EncryptionServiceProvider::class,
        Illuminate\Filesystem\FilesystemServiceProvider::class,
        Illuminate\Foundation\Providers\FoundationServiceProvider::class,
        Illuminate\Hashing\HashServiceProvider::class,
        Illuminate\Mail\MailServiceProvider::class,
        Illuminate\Notifications\NotificationServiceProvider::class,
        Illuminate\Pagination\PaginationServiceProvider::class,
        Illuminate\Pipeline\PipelineServiceProvider::class,
        Illuminate\Queue\QueueServiceProvider::class,
        Illuminate\Redis\RedisServiceProvider::class,
        Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
        Illuminate\Session\SessionServiceProvider::class,
        Illuminate\Translation\TranslationServiceProvider::class,
        Illuminate\Validation\ValidationServiceProvider::class,
        Illuminate\View\ViewServiceProvider::class,

        /*
         * Package Service Providers...
         */

        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        //App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,
        App\Providers\SocialMediaServiceProvider::class,

    ],

resources/views/layouts/sidebar.blade.php

<aside class="col-md-4 blog-sidebar">
  <div class="p-3 mb-3 bg-light rounded">
    <h4 class="font-italic">About</h4>
    <p class="mb-0">Etiam porta <em>sem malesuada magna</em> mollis euismod. Cras mattis consectetur purus sit amet fermentum. Aenean lacinia bibendum nulla sed consectetur.</p>
  </div>

  <div class="p-3">
    <h4 class="font-italic">Archives</h4>
    <ol class="list-unstyled mb-0">

      @foreach ($archives as $stats)

        <li>
          <a href="/?month={{ $stats['month'] }}&year{{ $stats['year'] }}">

            {{ $stats['month'] . ' ' . $stats['year'] }}

          </a>
        </li>

      @endforeach

    </ol>
  </div>

  <div class="p-3">
    <h4 class="font-italic">Tags</h4>
    <ol class="list-unstyled mb-0">

      @foreach ($tags as $tag)

        <li>
          <a href="/posts/tags/{{ $tag }}">

            {{ $tag }}

          </a>
        </li>

      @endforeach

    </ol>
  </div>

  <div class="p-3">
    <h4 class="font-italic">Elsewhere</h4>
    <ol class="list-unstyled">
      <li><a href="#">GitHub</a></li>
      <li><a href="#">Twitter</a></li>
      <li><a href="#">Facebook</a></li>
    </ol>
  </div>
</aside>
1
  • I feel like the boot method is not being called at all in the app service provider, I don't know why that would happen though. Commented Mar 4, 2018 at 13:17

4 Answers 4

4

Hi I copied and tried your code in the AppServiceProvider and it works fine, did you try clearing both your application cache and the service cache with :

php artisan config:clear
php artisan clear-compiled
Sign up to request clarification or add additional context in comments.

Comments

0

try to use View facade instand of view() method

App\Providers\AppServiceProvider.php

<?php

namespace App\Providers;

use App\Post;
use App\Tag;

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

class AppServiceProvider extends ServiceProvider
{

    protected $defer = true;

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        View::composer('layouts.sidebar', function ($view) {

            $view->with('archives', Post::archives());
            $view->with('tags', Tag::has('posts')->pluck('name'));

        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

1 Comment

I tried that but unfortunately I get the same error
0

I have ran into the same problem in past. I sorted out a way. But i don't know it's the best solution or not.

public function boot()
{
        view()->composer('*', function ($view) {

            $view->with('archives', Post::archives());
            $view->with('tags', Tag::has('posts')->pluck('name'));

        });
}

2 Comments

That doesn't seem to work for me either, I tried that and cleared the configuration but I get the same error.
You can try 'your_master_blade' instead of ' * '. This may work
0

This would work for you, and i believe that is what you want.

public function boot()
{
    \View::composer('*', function($view){
        $archives= Post::archives();
        $tags = Tag::has('posts')->pluck('name');
        $view->with('categories',$archives);
        $view->with('tags',$tags);
    });
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.