Php 组合函数中的Laravel cookie

Php 组合函数中的Laravel cookie,php,laravel,cookies,laravel-5,Php,Laravel,Cookies,Laravel 5,要构建我的菜单,我有一个文件Providers/viewcomoserviceprovider.php。在此文件中,我有: public function boot() { $this->composeNavigation(); } public function composeNavigation() { view()->composer('front.layouts.menu', function ($view) { $view->with

要构建我的菜单,我有一个文件Providers/viewcomoserviceprovider.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值?是否有特殊的方式将请求传递给我的组合导航功能

编辑我让它工作了,但我无法访问视图中的$brand()->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 );
    });
}

函数($view)使用($brand)
将使
$brand
在composer闭包的范围内可用。如果您想访问请求对象,可以使用IoC容器
$request=app('request')
或使用DI并仅添加
组合导航(Request$Request)
虽然有效,但cookie值不可读。就此问题提出了另一个问题