Variable undefined error in laravel blade view
Asked Answered
T

4

7

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.

Tachymetry answered 30/9, 2016 at 10:20 Comment(4)
Clearly explain question .. What you want.. ??Glyco
i am having a view which is does not have any controller only have Route like this Route::get('index', function () { return view('index'); }); in this view iam passing another view which is having its own controller and also having some data from an array. but after using this view inside the view i get undefined variable errorTachymetry
You need pass variable to viewMeteor
i am passing themTachymetry
D
27

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 : '' }}
Dijon answered 11/1, 2017 at 7:35 Comment(0)
C
4

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.

Cabinet answered 21/9, 2020 at 6:44 Comment(0)
C
0

You can try this:

 public function indexYourViews()
  { 
    $test = "Test Views";
    $secondViews = view('second',compact('test'));

     return view('firstview',compact('secondViews'));
   }

and after declare {{$secondViews}} in your main view file(firstview).

Hope this helps you.

Crysta answered 30/9, 2016 at 14:41 Comment(0)
D
-1
public function returnTwoViews() {
    $variable = 'foo bar';
    $innerView = view('inner.view', ['variable' => $variable]);

    return view('wrapper.view, ['innerView' => $innerView]);
}

This may be what you are looking for?

... inside your wrapper.view template:

{!! $innerView !!}

EDIT: to answer the question in the comment: In order to fetch each line you for do this inside your $innerView view:

@foreach($variable as $item)
    {{ $item }}
@endforeach

... and in the wrapper view it will still be {!! $innerView !!}

Dewaynedewberry answered 30/9, 2016 at 10:30 Comment(2)
thanks it works greatly...what if i have a array of $variable. then how would i fetch the in my wrapper view.Tachymetry
I have edited the original response because I need to include some more code for this case.Dewaynedewberry

© 2022 - 2024 — McMap. All rights reserved.