Search code examples
phplaravellaravel-5

How to pass the value from controller to header file


I need to pass the data from the controller to a header file inside partials folder.

Suggest me how to pass the data "currency" from the controller to the header file.

Controller :

class HeaderController extends Controller
{
   public function rate(){
     $currency = Whmcs::GetCurrencies([         
     ]);
     return view('partials.header',compact('currency'));        
   }
}

Header file:

<form name="form">
    <select name="currency" class="form-control">
        @foreach($currency['currencies']['currency'] as $key=>$value)
           @for($key=0;$key<100;$key++)
           @endfor
           <option value="{{$value['code']}}">{{$v=$value['code']}}</option>
        @endforeach
    </select>
</form>  

Route:

Route::any('/partials.header', 'HeaderController@rate');

Solution

  • You can make this via laravel view composer method as below:

    Add this method in App/Providers/ComposerServiceProvider.php in boot method

     view()->composer(['partials.header'], function ($view) {
     $currency = Whmcs::GetCurrencies([]);
         $view->with('currency', $currency);
     });
    

    Then you can use $currency variable to your header file you don't need to pass it from any controller.