Codington
Codington

Reputation: 115

Laravel view composer variable not defined using service provider

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>

Upvotes: 1

Views: 1493

Answers (4)

GabMic
GabMic

Reputation: 1482

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);
    });
}

Upvotes: 0

laleye Salomon
laleye Salomon

Reputation: 41

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

Upvotes: 4

Akib Tanjim
Akib Tanjim

Reputation: 16

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'));

        });
}

Upvotes: 0

Goms
Goms

Reputation: 2644

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()
    {
        //
    }
}

Upvotes: 0

Related Questions