在版本5中,当从控制器传递数据到刀片时出现未定义变量错误


Undefined Variable error while passing data from controller to blade in laravel 5

好的,

这是UserControllerindex()方法

public function index()
{
    $name = 'echoashu';
     return view('home', compact('name'));
}

就这么简单,这里我的home.blade.php代码

  <span class="info-box-number">{{$name}} </span>

这必须按照文档理想地工作,但是它返回未定义的变量error

Undefined variable: name (View: C:'xampp'htdocs'laravel1'resources'views'home.blade.php)

猜吗? ?

给人一条鱼,你喂他一天;授人以渔,授人以渔

下面让我来说明在这种情况下如何调试(fish)

第一步:

确保你的呼叫是正确的

你可以通过

Route::get('yourcall', 'UserController@index');

在将其传递到视图中之前,只需在控制器中打印一些内容,例如

public function index()
{
echo 'Whao ! I landed correctly';
}

第2步:

确保你看到了你所说的

现在返回到视图,确保您的视图存在并且具有扩展名为yourview.blade.php

的名称

你可以通过

return view('yourview', compact($YourValue));

你应该有一个名为yourview.blade.php

的视图

在叶片内部,您可以获得传递的值,如

{{$YourValue}}  // If you have your file name as yourview.blade.php

<?php
echo $YourValue // If you have your file name as yourview.php
?>