Laravel 5.1 : Passing Data to View Composer

前端 未结 3 575
天命终不由人
天命终不由人 2021-02-06 15:20

I\'m using view composers in Laravel 5.1: nice. But how can I pass parameters to a view composer?

In my case I send week info (previous, current, next week, inc

3条回答
  •  鱼传尺愫
    2021-02-06 15:45

    All the data you pass to your view in the controller will be available in the view controller. Use the getData method on the view instance like this:

    $view->getData()["current_week"];
    

    In your particular case, you can do this:

    public function compose(View $view)
    {
       $current_week = $view->getData()["current_week"];
       //use $current_week as desired 
    }
    

    You can also get the data in the route (route parameters) from the request like this:

    request()->route()->getParameter('week_number');
    

提交回复
热议问题