Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

luddinus's avatar

Issue when the SQL query is slower than multiple requests

Hi.

I have a page where an user can upload some photos.

Imagine that the limit is ten photos per user. I use dropzone to upload multiple photos, and it runs one request per photo upload.

The problem is that when I check the number of the photos inthe request, when the next request "starts", sometimes the query has not finished, so the user "sometimes" can upload more than ten photos. (I know I could do some front-end validation, but does not solve my problem.

// PhotoController
public function store()
{
   // check...
   if (request()->user()->countPhotos() >= 10)
   {
      return response()->json([
         'message' => 'You cannot upload more than 10 photos'
      ], 403);
   }

   // upload the photo...
}

What can I do to solve this?

0 likes
4 replies
bobbybouwmann's avatar

I think the best solution is doing something with JS. Another solution would be keeping the count in a session or cookie or whatever. You can higher up the count before you start the upload and check if the count has been reached. You can also store that in the database, but that might be a bit slower!

1 like
Snapey's avatar

increment a session variable?

session(['images' => session('images',0)+1]);

and then test the value and if over 10, show an error

1 like
luddinus's avatar

Thanks for the response. I will try with cookie/session approach.

Please or to participate in this conversation.