【问题标题】:Passing and printing a value of string from controller to views in laravel将字符串值从控制器传递并打印到 laravel 中的视图
【发布时间】:2017-01-09 22:20:30
【问题描述】:

这可能是一个非常简单的问题。但我被卡住了。这是我的控制器函数,我试图将视图中的列总和传递到表的末尾

public function totalBillc3()
    {
      $total = Collection::where('collector_id', '=', 3)->sum('package');

      return View::make('users.collector3', compact('total',$total));



    }

在我写的观点中

             <tr>
               <td colspan="4" class="noborders"></td>
               <th class="text-right" scope="row">TOTAL</th>
               <td class="text-right">{{ $total}}</td>
            </tr>

我的路线设置完美,但显示错误

Undefined variable: total (View: /Volumes/G/zipbillingsoft.com/resources/views/users/collector3.blade.php).    Please help.

【问题讨论】:

    标签: laravel controller blade


    【解决方案1】:

    compact() 将一个或多个字符串作为参数,然后查找以这些字符串命名的变量。

    换句话说,你不应该这样做

    compact('total', $total)
    

    只是

    compact('total')
    

    如果你有多个变量,做

    compact('total', 'something', 'something_else')
    

    文档:http://php.net/compact

    【讨论】:

    • 如何在我的视图中打印?
    • {{ $total }} 和你一模一样。
    【解决方案2】:

    你在 View::make 中使用函数 compact 时犯了一个错误。试试下面的代码:

    return View::make('users.collector3', compact('total',['total']));
    

    【讨论】:

    • 如何在我的视图中打印?
    猜你喜欢
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多