title: Using Laravel Blade in Javascript with Dropzone -> Max total Files uploaded
body: <p>I'm using Dropzone within my Laravel application. In Dropzone, I can limit the maximum number of files uploaded at one time. However, after refreshing the page, users can then upload more photos. </p>
  
  <pre><code>Dropzone.options.addPhotosForm = {
    maxFiles: 5,
  };
  </code></pre>
  
  <p>I am able to remove this ability on the front end by counting the number of photos that this user has already uploaded for this page and then changing the class to display:none; and giving a warning as follows:</p>
  
  <pre><code>&lt;div class="@if(count($rooms-&gt;photos) &gt;= 10) d-none @endif"&gt;
    &lt;form action="{{ route('myroute', [$rooms-&gt;slug]) }}" method="POST" class="dropzone" id="addPhotosForm"&gt;
      {{ csrf_field() }}
    &lt;/form&gt;
  &lt;/div&gt;
  &lt;div class="@if(count($rooms-&gt;photos) &lt; 10) d-none @endif"&gt;
    &lt;p&gt;You have uploaded {{ count($rooms-&gt;photos)}} photos. This is too many.&lt;/p&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>The problem here is that if a user has uploaded 9 photos, they can then upload 5 more and get a total of 14 instead of 10 photos, which is the desired maximum. Furthermore, if the user is malicious, they can simply go to Inspect Elements, remove the display: none; style and upload as many photos as they want.</p>
  
  <p>I would like to disable a user's ability to upload more photos within the Javascript and I am trying to use Laravel's Blade to do this, but with some problems.</p>
  
  <p>So, I have a few questions: <br><br>
  <strong>1) Can I use Blade within Javascript?</strong> </p>
  
  <p>I have tried this by, for example using: </p>
  
  <pre><code> var count = count($rooms-&gt;photos);
  </code></pre>
  
  <p>I then get the error, </p>
  
  <blockquote>
    <p>Uncaught SyntaxError: Unexpected token ></p>
  </blockquote>
  
  <p>If I could get this to work, my idea was to do this within the Dropzone options.</p>
  
  <pre><code>init: function(file) {
    if (count &gt;= 10){
        myDropzone.removeFile(file);
    };
  </code></pre>
  
  <p><strong>2) Is there be a better way to limit all uploads to Dropzone?</strong>
  <br>
  I have tried a few of the suggestions here on SO with no luck. </p>
  
  <p>Thanks!</p>
  
  <p>Brad</p>
  
  <p><strong>EDIT: SOLUTION</strong></p>
  
  <p>Thanks @Lukas and @Miguel Cabrera for your quick replies!</p>
  
  <p>I have adopted a solution based on your input.</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
    var count = {{ count($rooms-&gt;photos) }};
    var maxFiles = 10 - {{ count($rooms-&gt;photos) }};
  &lt;/script&gt;
  
   &lt;script type="text/javascript"&gt;
     Dropzone.options.addPhotosForm = {
      maxFiles: maxFiles,
     }
   &lt;/script&gt;
  </code></pre>
  
  <p>This works perfectly. Thanks again!! :) </p>