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>
  • 写回答

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
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘