drnvcm3949
2015-06-06 07:49
浏览 54
已采纳

在laravel 5中将数据从控制器传递到刀片时未定义可变错误[重复]

Allright,

Here is my index() method of UserController

public function index()
{

    $name = 'echoashu';

     return view('home', compact('name'));
}

As simple as that, and here my home.blade.php code

  <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\laravel1esources\views\home.blade.php)

Any guess??

</div>

图片转代码服务由CSDN问答提供 功能建议

此问题已经存在 这里有一个答案:

  • ”注意:未定义的变量“,”注意:未定义的索引“和”通知:未定义的偏移“使用PHP 28 answers

    好吧,

    这是 UserController

      index()方法 > public function index()
     {
     
     $ name ='echoashu'; 
     
    返回视图('home',compact('name')); 
    } 
       
     
     

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

     &lt; span class =“  info-box-number“&gt; {{$ name}}&lt; / span&gt; 
       \  n 
     

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

     未定义的变量:name(查看:C:\ xampp \ htdocs \ laravel1 \  resources \ views \ home.blade.php)
       
     
     

    任何猜测??

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • dsa4d4789789 2015-06-06 08:05
    最佳回答

    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

    You can do it by

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

    So, You should have a view named as 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
    ?>
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题