lundi 29 avril 2019

View Composer not working in my Laravel App

I am trying to share variables on multiple views so i tried the view composer but it is not working as its not passing variables and is there any way to debug it

ComposerServiceProvider.php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class ComposerServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        view()->composer(
            'layouts.admin.dashboard',
            'App\Http\ViewComposers\StatComposer'
        );
    }

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

So now here will be the Composer File

StatComposer.php

   <?php

namespace App\Http\ViewComposers;

use Analytics;
use Spatie\Analytics\Period;
use App\Libraries\GoogleAnalytics;
use Illuminate\Http\Request;
use Illuminate\View\View;

class StatComposer
{
    /**
     * Create a movie composer.
     *
     * @return void
     */
    public function __construct()
    {
        $result = GoogleAnalytics::topCountries();
        $country = $result->pluck('country');
        $country_sessions = $result->pluck('sessions');

        $topBrowsers = GoogleAnalytics::topBrowsers();
        $browser = $topBrowsers->pluck('browser');
        $browser_sessions = $topBrowsers->pluck('sessions');

        $totalPageViews = GoogleAnalytics::fetchVisitorsAndPageViews();
        $date = $totalPageViews->pluck('date');
        $visitors = $totalPageViews->pluck('visitors');
        $pageViews = $totalPageViews->pluck('pageViews');
    }

    /**
     * Bind data to the view.
     *
     * @param  View  $view
     * @return void
     */
    public function compose(View $view)
    {
        $view->with('country', 'country_sessions', 'browser', 'browser_sessions','date','visitors','pageViews');
    }
}

So I am unable to find way to debug it as the variables are not passing to the view i am trying to pass them to and the view is giving error of undefined variable.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire