laravel Laravel刀片视图中的变量未定义错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39789211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:32:29  来源:igfitidea点击:

Variable undefined error in laravel blade view

laravelviewbladelaravel-5.3

提问by Priyank lohan

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.

我有一个没有任何控制器的视图,Route::get('index', function () { return view('index'); });在这个视图中只有这样的 Route我正在传递另一个视图,它有自己的控制器,也有一些来自数组的数据。但在视图中使用此视图后,我收到未定义的变量错误。

回答by m0z4rt

two steps.

两步。

  1. Declare & transfer $variableto View from Controllerfunction.

    public function index() { return view("index", [ "variable" => $variable ]); }

  2. Indicate where transferred $variablefrom Controllerappear in view.blade.php.

    {{ $variable }}

  1. 控制器函数声明并传输$variable到 View 。

    public function index() { return view("index", [ "variable" => $variable ]); }

  2. 指示从Controller传输的$variable出现在view.blade.php 中的位置

    {{ $变量}}

if you don't make sure, $variable is transferred or not

如果你不确定, $variable 是否被转移

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

回答by AddWeb Solution Pvt Ltd

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).

{{$secondViews}}在您的主视图文件(firstview)中声明之后。

Hope this helps you.

希望这对你有帮助。

回答by renov8

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:

...在您的 wrapper.view 模板中:

{!! $innerView !!}

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

编辑:回答评论中的问题:为了获取您在$innerView视图中执行此操作的每一行:

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

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

...在包装视图中它仍然是 {!! $innerView !!}