Laravel - 如何传递数据以包含
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37075838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - How to pass data to include
提问by Codearts
So essentially all of my views are using the header.blade.php since I am including it in my master layout. I need to pass data to the header on every single view. So is there a way to pass data just to the include rather than passing the data for the header in each view?
所以基本上我所有的视图都使用 header.blade.php,因为我将它包含在我的主布局中。我需要将数据传递给每个视图的标题。那么有没有办法只将数据传递给包含而不是在每个视图中传递标题的数据?
采纳答案by Jeff Lambert
One option if you're trying to send data onlyto the included view is to use a view composer. They will fire even in the case of trying to prepare a view for @include
如果您尝试仅将数据发送到包含的视图,则一种选择是使用视图 composer。即使在尝试准备视图的情况下,他们也会开火@include
view()->composer('header', function($view) {
$view->with('data', 'some data');
});
回答by Alexey Mezenin
You don't need to do that, but you can:
你不需要这样做,但你可以:
All variables that are available to the parent view will be made available to the included view. Even though the included view will inherit all data available in the parent view, you may also pass an array of extra data to the included view:
@include('view.name', ['some' => 'data'])
父视图可用的所有变量都可用于包含的视图。即使包含视图将继承父视图中可用的所有数据,您也可以将一组额外数据传递给包含视图:
@include('view.name', ['some' => 'data'])