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.

4
  • Clearly explain question .. What you want.. ?? Commented Sep 30, 2016 at 10:22
  • 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 error Commented Sep 30, 2016 at 10:29
  • You need pass variable to view Commented Sep 30, 2016 at 10:39
  • i am passing them Commented Sep 30, 2016 at 10:50

4 Answers 4

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 : '' }}
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

Comments

-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 !!}

2 Comments

thanks it works greatly...what if i have a array of $variable. then how would i fetch the in my wrapper view.
I have edited the original response because I need to include some more code for this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.