laravel 在laravel 5中将数据从控制器传递到刀片时出现未定义的变量错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30680336/
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
Undefined Variable error while passing data from controller to blade in laravel 5
提问by echoashu
Allright,
好的,
Here is my index()
method of UserController
这是我的index()
方法UserController
public function index()
{
$name = 'echoashu';
return view('home', compact('name'));
}
As simple as that, and here my home.blade.php
code
就这么简单,这里是我的home.blade.php
代码
<span class="info-box-number">{{$name}} </span>
This must work ideally as per documentation, but it returns undefined variable error
这必须按照文档理想地工作,但它返回未定义的变量错误
Undefined variable: name (View: C:\xampp\htdocs\laravel1\resources\views\home.blade.php)
Any guess??
有什么猜想吗??
回答by Sulthan Allaudeen
Give a man a fish and you feed him for a day; Teach a man to fish and you feed him for a lifetime
给一个人一条鱼,你就能养活他一天;教一个人钓鱼,你养活他一辈子
Here's Let me say how to debug(fish) in this situation.
下面让我说一下在这种情况下如何调试(钓鱼)。
1st Step :
第一步:
Make sure that your call is right
确保你的电话是正确的
You can do it by
你可以通过
Route::get('yourcall', 'UserController@index');
Before passing it inside the view, Just print something inside your controller like
在将它传递到视图中之前,只需在控制器中打印一些内容,例如
public function index()
{
echo 'Whao ! I landed correctly';
}
2st Step :
第二步:
Make sure that you see what you call
确保您看到您调用的内容
Now make your return to the view, Make sure that your view exists and have the name with extension like yourview.blade.php
现在返回视图,确保您的视图存在并且具有扩展名的名称,例如 yourview.blade.php
You can do it by
你可以通过
return view('yourview', compact($YourValue));
So, You should have a view named as yourview.blade.php
所以,你应该有一个名为 yourview.blade.php
Inside the blade you can get the passed value like
在刀片内部,您可以获得传递的值,例如
{{$YourValue}} // If you have your file name as yourview.blade.php
or
或者
<?php
echo $YourValue // If you have your file name as yourview.php
?>