Soib
Soib

Reputation: 23

Undefined variable when sending data to a view in Laravel 5.8

I have the following problem: I load the main layout (view) of the site in the renderOutput method which is written on the parent controller SiteController.

protected function renderOutput(){
  $menu = $this->getMenu();
  $navigation = view(env('THEME').".navigation")->with('menu',$menu)->render();
  $this->vars = array();
  $this->vars = array_add($this->vars,'navigation', $navigation);

  return view($this->template)->with($this->vars);
}

And in the child controller IndexController in the index method, I call this method. And also to display the section (section) sliders in the index method, I pass the $sliders variable to the address of the slider template.

public function index(){
    //
    $sliders = view(env('THEME').".slider")->render();
    $this->vars = array();
    $this->vars = array_add($this->vars,'sliders',$sliders); 

    return $this->renderOutput();
}

But when I use this variable in the template, I get an error. That is, the $sliders variable is not available in the template. Please tell me how to solve the problem. Thank you in advance!

Undefined variable: sliders (View: C:\Users\User\Downloads\Programs\OSPanel\domains\Corporate\resources\views\pink\index.blade.php)

Upvotes: 2

Views: 497

Answers (1)

Andrei Lupuleasa
Andrei Lupuleasa

Reputation: 2762

In renderOutput() you are overwriting $this->vars that is why it is undefined.

Use:

protected function renderOutput(){
  $menu = $this->getMenu();
  $navigation = view(env('THEME').".navigation")->with('menu',$menu)->render();
  $this->vars = array_add($this->vars,'navigation', $navigation);

  return view($this->template)->with($this->vars);
}

Upvotes: 1

Related Questions