【问题标题】:Laravel Blade fails in forelse, works in foreachLaravel Blade 在 forelse 中失败,在 foreach 中工作
【发布时间】:2018-02-06 09:09:21
【问题描述】:

我正在将 Laravel 从 5.2 升级到 5.3,但我的一个 Blade 视图不再工作。我将一个数组传递给包含的视图以循环遍历它。我正在使用 forelse 指令,但它一直给我一个未定义的偏移量:1 错误。

这是带有视图调用的控制器 sn-p:

$transactions = $committees->transactions()
    ->where('FiledDate', '<=', $to)  // Upper date
    ->where('FiledDate', '>=', $from)  // Lower date
    ->get();

return view('committees.show',
    [ 
        'data' => $data,
        'transactions' => $transactions,
    ]);

这是 Blade 文件。

<table class="table table-striped">
<thead>
    <tr><th class="text-center" colspan="5">Transactions</th></tr>
    <tr>
        <th class="text-center">TranId</th>
        <th class="text-center">Tran Date</th>
        <th class="text-center">SubType</th>
        <th class="text-center">Filed Date</th>
        <th class="text-center">Amount</th>
    </tr>
</thead>
<tbody>
@forelse ($transactions AS $transaction)
    <tr>
        <td class="text-center">{{ $transaction->TranId }}</td>
        <td class="text-center">{{ $transaction->TranDate }}</td>
        <td class="text-center">{{ $transaction->SubType }}</td>
        <td class="text-center">{{ $transaction->FiledDate }}</td>
        <td class="text-center">{{ number_format($transaction->Amount, 2, '.', ',') }}</td>
    </tr>
@empty
    <tr><td colspan="5">No Transactions</td></tr>
@endforelse
</tbody>              

我创建了一个虚拟事务数组,但仍然出现同样的错误。

此外,当我使用 foreach 指令时,它可以正常工作,但我必须进行额外的测试以检查是否没有记录。

【问题讨论】:

    标签: laravel laravel-blade laravel-5.3


    【解决方案1】:

    失败的不是迭代的执行,而是 Blade 模板的编译。差异对于调试问题至关重要。在18th of September 之前,forelse 指令的编译是区分大小写的,这意味着当它尝试编译您的语句时,它无法生成输出所需的实际 PHP 代码所需的匹配项。更新 Laravel 应该可以解决这个问题。

    所以,澄清一下,这会破坏:

    @forelse ($transactions AS $transaction)
    

    虽然这可行:

    @forelse ($transactions as $transaction)
    

    话虽如此,我强烈建议您关注PSR-2并写下所有PHP keywords in lowercase

    【讨论】:

      【解决方案2】:

      您是否尝试过转储交易的内容?它可能没有您在 forelse 循环中访问的字段,从而导致错误。

      尝试在您的 forelse 循环中执行 {{var_dump($transaction)}} 并确保您的代码中包含您正在访问的所有字段。

      【讨论】:

      • 是的,我已经 var_dumped $transaction 变量。目前我已将代码更改为 foreach 并测试了一个空变量。
      【解决方案3】:

      在控制器中以这种方式创建 $transaction: $transacrion 不是数组: 使用此代码:

      $transaction = Transaction::all();
      

      【讨论】:

      • 我将模型调用添加到我原来的问题中。真正让这变得困难的是 foreach 循环工作得很好。
      【解决方案4】:

      您只需要更新您的@forelse 部分,如下所述:

      @forelse ($transactions ? $transactions : [] as $transaction)
          <tr>
              <td class="text-center">{{ $transaction->TranId }}</td>
              <td class="text-center">{{ $transaction->TranDate }}</td>
              <td class="text-center">{{ $transaction->SubType }}</td>
              <td class="text-center">{{ $transaction->FiledDate }}</td>
              <td class="text-center">{{ number_format($transaction->Amount, 2, '.', ',') }}</td>
          </tr> @empty
          <tr><td colspan="5">No Transactions</td></tr> @endforelse
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-01-10
        • 2020-03-19
        • 2018-06-07
        • 2014-02-24
        • 2018-08-04
        • 2015-07-10
        • 2014-08-08
        • 2018-03-07
        相关资源
        最近更新 更多