【问题标题】:Pass data to included blade view将数据传递到包含的刀片视图
【发布时间】:2019-09-01 17:28:07
【问题描述】:
假设我有以下主要观点:
<div>
<div>
@yield('content')
</div>
<div>
@include('sidebar')
</div>
<div>
@include('footer')
</div>
</div>
如何确保包含的视图有自己的数据?例如,在我想要显示的侧边栏中,比如说,新的博客文章 cmets。在页脚中,我想显示新用户和博客文章。也许使用@include 不是实现此目的的正确方法?
【问题讨论】:
标签:
laravel
laravel-blade
【解决方案1】:
在您的应用程序中,应用服务提供商的引导方法会执行类似的操作
View::composer('sidebar',function($view){
$comments= Blog::latest()->comments;
$view->with('comments',$comments);
})
View::composer('footer',function($view){
$users= User::latest();
$view->with('users',$users);
})
//侧边栏
然后在视图文件中执行类似的操作
@foreach($comments as $comment)
{{$comment->data}}
@endforeach`
【解决方案3】:
将您的逻辑写入View Composer,然后在启动方法中使用服务提供者调用它
View::composer(
['partials.content', 'partials.sidebar', 'partials.footer], 'App\Http\ViewComposers\MyViewComposer'
);
【解决方案4】:
我试过了,它对我有用:
@include('content',array("key"=>"value"))