douhan0562 2017-07-28 19:45
浏览 7
已采纳

如何将一些变量只放到某些特定的视图中?

I found in documentation that something similar can be done

But for all views

By putting in AppServiceProvider or other own ServiceProvider something like

View::share('key', 'value');

But how to share data only with some specific views?

Not all. Not one. But some views? Views listed im array or something that I want it to share

  • 写回答

2条回答 默认 最新

  • doufu8127 2017-07-28 19:52
    关注

    This is how I usually set up this kind of situation

    In AppServiceProvider

    public function boot()
    {
          View::composer(
            ['home', 'profile.edit', 'projects.create'], // array of views
            function($view){
              $foo = \App\Foo::where('active', 1)->get();
              $bar = \App\Bar::all();
              $view->with(compact('foo', 'bar'));
            }
          );
    }
    

    Then in the views I passed to the array,I can reference my variables:

    @foreach($foo as $item) :
    
    @endforeach
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题