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

报告相同问题?

悬赏问题

  • ¥23 (标签-bug|关键词-密码错误加密)
  • ¥66 比特币地址如何生成taproot地址
  • ¥20 数学建模数学建模需要
  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决