kkatwork
2 months ago
243
2
Laravel

How to use old Service providers in Laravel 7?

Posted 2 months ago by kkatwork

Hi, I have a 5.4 laravel project, which I want to upgrade to laravel 7. I have created a new Laravel 7 project and copied the required files from old project including providers. It is medium sized project.

I have one provider which attaches logged-in-user to every view. But, my views throws an undefined variable 'logged-in-user' error. I can not make it optional because it is used in almost every view.

I guess the service provide isn't working with laravel 7. Composer service provider

class ComposerServiceProvider extends ServiceProvider
{
    /**
     * Register bindings in the container.
     *
     * @return void
     */
    public function boot()
    {

        /*
         * Global
         */
        View::composer(
            // This class binds the $logged_in_user variable to every view
            '*', GlobalComposer::class
        );

        /*
         * Frontend
         */

        /*
         * Backend
         */
        View::composer(
            // This binds items like number of users pending approval when account approval is set to true
            'backend.includes.sidebar', SidebarComposer::class
        );
    }

GlobalComposer class

<?php

namespace App\Http\Composers;

use Illuminate\View\View;

/**
 * Class GlobalComposer.
 */
class GlobalComposer
{
    /**
     * Bind data to the view.
     *
     * @param View $view
     *
     * @return void
     */
    public function compose(View $view)
    {

        $view->with('logged_in_user', access()->user());
    }
}

Please sign in or create an account to participate in this conversation.