dongtaogou6226 2019-04-29 20:59
浏览 35

View Composer无法在我的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.

  • 写回答

1条回答 默认 最新

  • douhutongvm382381 2019-04-29 21:34
    关注

    The issue is your declaring a lot of variables without providing the values in the compose function. Also your not actually saving the data anywhere. It should be like this.

    protected $country;
    
    /**
     * Create a movie composer.
     *
     * @return void
     */
    public function __construct()
    {
        $result = GoogleAnalytics::topCountries();
        $this->country = $result->pluck('country');
    }
    
    /**
     * Bind data to the view.
     *
     * @param  View  $view
     * @return void
     */
    public function compose(View $view)
    {
        $view->with('country', $this->country);
    }
    

    Note we are declaring the classes property at the top, then saving to that property and then setting the 'country' variable in the with method with the $this->country property.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据