Laravel-尝试从blade.php文件中有条件地设置PDF文件的内容

There is a feature on a Laravel/ PHP site I'm working on that allows a user to download a payment reminder letter as a PDF file- the PDF is generated based on properties belonging to the database entry that they choose to download the PDF for.

The reminder.blade.php file that is generated for the download originally looked like this:

<html>
<head>
    ...
</head>
<body style="....">
    @foreach ($transaction as $transaction)
        ...
        <!-- Content of the PDF here -->
    @endforeach
</body>

I want to change what the PDF displays based on the number/type of transactionItems in the $transaction that is being exported to the PDF- if there is only one transactionItem in the $transaction with a $transactionItem.currentStatusId property value of 1010 (an ID of a particular value of the $transactionItem.currentStatusId field, only the first page of the PDF should be generated, otherwise, all pages should be generated (as they currently are)

To do this, I have tried to surround the <body></body> tags with an @if that checks the number of transactionItems in the $transaction that's being exported, and set the content of the PDF appropriately within each of the @if & @else statement, so the changes I made to the reminder.blade.php file mean it currently looks like this:

<html>
<head>
    ...
</head>
@if(($transaction->count($transaction['transactionItem']) == 1) && ($transaction['transactionItem']['currentStatusId'] == '1010'))
    <body style="....">
        @foreach ($transaction as $transaction)
            ...
            <!-- First page of PDF only -->
        @endforeach
    </body>
@else
    <body style="...">
        @foreach ($transaction as $transaction)
            ...
            <!-- Original PDF here -->
        @endforeach
    </body>
@endif
</html>

However, when I now export the PDF file, although the file downloads successfully, when I try to open it, I get an error that says:

Failed to load PDF document.

Anyone have any idea why I might be getting this? I assume there's something I'm doing wrong with the @if statement I'm using to generate the content dynamically, but I can't see what... Can anyone point me in the right direction? How can I alter the content of the PDF generated based on the value of a field in the database, which is being used to generate the PDF?