Variable undefined error in laravel blade view

爱⌒轻易说出口 提交于 2019-12-11 04:04:30

问题


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.


回答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 don't make sure, $variable is transferred or not

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



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/39789211/variable-undefined-error-in-laravel-blade-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!