title: Laravel- Trying to set the content of a PDF file conditionally from within blade.php file
body: <p>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.</p>
  
  <p>The <code>reminder.blade.php</code> file that is generated for the download originally looked like this:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      ...
  &lt;/head&gt;
  &lt;body style="...."&gt;
      @foreach ($transaction as $transaction)
          ...
          &lt;!-- Content of the PDF here --&gt;
      @endforeach
  &lt;/body&gt;
  </code></pre>
  
  <p>I want to change what the PDF displays based on the number/type of <code>transactionItems</code> in the <code>$transaction</code> that is being exported to the PDF- if there is only one <code>transactionItem</code> in the <code>$transaction</code> with a <code>$transactionItem.currentStatusId</code> property value of <code>1010</code> (an ID of a particular value of the <code>$transactionItem.currentStatusId</code> field, only the first page of the PDF should be generated, otherwise, all pages should be generated (as they currently are)</p>
  
  <p>To do this, I have tried to surround the <code>&lt;body&gt;&lt;/body&gt;</code> tags with an <code>@if</code> that checks the number of <code>transactionItems</code> in the <code>$transaction</code> that's being exported, and set the content of the PDF appropriately within each of the <code>@if</code> &amp; <code>@else</code> statement, so the changes I made to the <code>reminder.blade.php</code> file mean it currently looks like this:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
      ...
  &lt;/head&gt;
  @if(($transaction-&gt;count($transaction['transactionItem']) == 1) &amp;&amp; ($transaction['transactionItem']['currentStatusId'] == '1010'))
      &lt;body style="...."&gt;
          @foreach ($transaction as $transaction)
              ...
              &lt;!-- First page of PDF only --&gt;
          @endforeach
      &lt;/body&gt;
  @else
      &lt;body style="..."&gt;
          @foreach ($transaction as $transaction)
              ...
              &lt;!-- Original PDF here --&gt;
          @endforeach
      &lt;/body&gt;
  @endif
  &lt;/html&gt;
  </code></pre>
  
  <p>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:</p>
  
  <blockquote>
    <p>Failed to load PDF document.</p>
  </blockquote>
  
  <p>Anyone have any idea why I might be getting this? I assume there's something I'm doing wrong with the <code>@if</code> 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?</p>