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

图片转代码服务由CSDN问答提供 功能建议

我在文档中发现可以做类似的事情

但是对于所有人来说 视图

通过放入AppServiceProvider或其他自己的ServiceProvider类似

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

但是如何仅与某些特定的视图共享数据?

并非所有。 不是一个。 但有些意见? 列出的视图是数组或我希望它共享的内容

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • drh96824 2017-07-28 19:47

    Use view composer.

    You may attach a view composer to multiple views at once by passing an array of views as the first argument to the composer method

    View::composer(
        ['profile', 'dashboard'],
        'App\Http\ViewComposers\MyViewComposer'
    );
    

    https://laravel.com/docs/5.4/views#view-composers

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题