Variable undefined error in laravel blade view

25,838

Solution 1

Two steps :

  1. Declare & transfer $variable to View from Controller function.

     public function index()
     {
      return view("index", [ "variable" => $variable ]);
     }
    
  2. Indicate where transferred $variable from Controller appear in view.blade.php.

     {{ $variable }}
    

If you do not make sure, $variable is transferred or not

{{ isset($variable) ? $variable : '' }}

Solution 2

If this helps anyone, I was completely ignorant to the fact that my route was not hooked with the corresponding controller function and was returning the view directly instead, thereby causing this issue. Spent a good half hour banging my head till I realized the blunder.

Edit

Here again to highlight another blunder. Make sure you're passing your array correctly. I was doing ['key', 'value] instead of ['key' => 'value'] and getting this problem.

Share:
25,838
Priyank lohan
Author by

Priyank lohan

Updated on November 18, 2020

Comments

  • Priyank lohan
    Priyank lohan over 3 years

    Hey as i am passing a blade view which is having it own controller also i am including it into the view which does not have its own controller. it gives me an undefined variable error can any one help me how to it.

    I have a view which does not have any controller only have Route like this Route::get('index', function () { return view('index'); }); in this view i am passing another view which having its own controller and also having some data from an array. but after using this view inside the view i get undefined variable error.