要构建我的菜单,我有一个文件Providers / ViewComposerServiceProvicer.php。在这个文件中我有:
public function boot()
{
$this->composeNavigation();
}
public function composeNavigation()
{
view()->composer('front.layouts.menu', function ($view) {
$view->with('menuItems', \App\MenuItem::orderBy('priority', 'asc')->get());
});
}
我想要的是根据Coockie值添加where查询,如:
$brand = $request->cookie('brand');
// if value not set use default value.
if($brand == null)
{
$brand = 1;
}
view()->composer('front.layouts.menu', function ($view) {
//extra where function
$view->with('menuItems', \App\MenuItem::Where('brand','=',$brand)->orderBy('priority', 'asc')->get());
});
如何在compose函数中获取coockie值?有没有一种特殊的方法可以将Request传递给我的composeNavigation函数?
编辑我得到它的工作,但我无法访问视图中的$品牌() - > composer(),如果我在我无法访问请求的功能中复制我的代码
我的更新代码:
public function boot(Request $request)
{
$this->composeNavigation($request);
}
public function composeNavigation(Request $request)
{
$coockieValue = $request->cookie('brand');
// if value not set use default value.
if($coockieValue == null)
{
$coockieValue = null;
}
$brands = \App\Brand::orderBy('priority', 'asc')->get();
foreach($brands as $brand){
if($brand->id == $coockieValue){
$brand->menuActive = true;
}
else{
$brand->menuActive = false;
}
}
view()->composer('front.layouts.menu', function ($view) {
$view->with('brandItems',$brands );
});
}