title: How can I optimize my Laravel controller to avoid 504?
body: <p>I'm trying to push around 3,000 items into a table - I'm not sure if its possible but I feel my controller could reach that level with some optimizations. It's currently taking 60+ seconds and timing out.</p>
  
  <pre><code>if ($request-&gt;has('items')) {
      $queueData = [];
      foreach ($request-&gt;get('items') as $item) {
          if (
              QueueItem::where('item', $item)-&gt;exists() ||
              QueueItemFailed::where('item', $item)-&gt;exists() ||
              CacheItem::where('item', $item)-&gt;exists()
          ) {
              continue;
          }
  
          $queueData[] = ['item' =&gt; $item];
      }
  
      QueueItem::insert($queueData);
  
      return response()-&gt;json([
          'success' =&gt; true,
      ], 200);
  }
  </code></pre>