简体   繁体   中英

Laravel Blade fails in forelse, works in foreach

I am upgrading Laravel from 5.2 to 5.3 and one of my Blade views is no longer working. I am passing an array to an included view to loop through it. I am using the forelse directive but it keeps giving me an Undefined offset: 1 error.

Here is the controller snippet with the view call:

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

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

Here is the Blade file.

<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>              

I have created a dummy transaction array but I still got the same error.

Also, when I use the foreach directive it works fine but then I have to have an additional test of checking for no records.

Have you tried vardumping the contents of transaction? It might not have the fields you are accessing in your forelse loop, thereby causing the error.

Try doing a {{var_dump($transaction)}} in your forelse loop and make sure you have all the fields you are accessing in your code.

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

$transaction = Transaction::all();

It's not the execution of your iteration that fails, but rather the compiling of your Blade template. The difference is crucial for debugging the issue. Up until de 18th of September the compiling of forelse directives was case-sensitive, meaning that when it tried to compile your statement, it failed to produce the matches it needed to output the actual PHP code you needed. Updating Laravel should fix the issue.

So, to clarify, this would break:

@forelse ($transactions AS $transaction)

While this would work:

@forelse ($transactions as $transaction)

Having said that, I'd strongly advise you to follow PSR-2 and write all PHP keywords in lowercase .

You just need to update your @forelse section like I mentioned below:

@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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM