【问题标题】:Laravel 5 - how to send data to layout views?Laravel 5 - 如何将数据发送到布局视图?
【发布时间】:2017-10-19 18:35:05
【问题描述】:

我必须将数据发送到布局,每次打开站点时它都会起作用,它添加了布局(不仅在 mainSite 上,也在链接上)。 我尝试过这样的事情(我添加到函数 boot ):

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    view()->composer('layout', function($view)
    {
        $categories = Category::all();
        $view->with('categories', $categories);
    });

}

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

}

但我仍然有错误: 未定义变量:类别

(View: 
/laravel/shop/resources/views/layout.blade.php) (View: 
/laravel/shop/resources/views/layout.blade.php)

如何配置?

【问题讨论】:

    标签: laravel layout view config


    【解决方案1】:

    对不起,我发现我的错误 - 我忘了在顶部添加: 使用应用\类别; 主题关闭。

    【讨论】:

      【解决方案2】:

      试试这样的东西

      view(*)->composer('layout', function($view)
      {
          $categories = Category::all();
          $view->with('categories', $categories)
      });
      
      view()->share('categories ', $categories );
      

      【讨论】:

        【解决方案3】:

        在视图中:

        @extends('some-layout',array('foo' =&gt; 'bar'))

        您可以在布局中使用$foo 变量。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-07-15
          • 1970-01-01
          • 1970-01-01
          • 2017-01-17
          • 2021-12-17
          • 1970-01-01
          • 2019-11-13
          • 1970-01-01
          相关资源
          最近更新 更多