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

ricocrivelli's avatar

Avoiding duplicate records with multiple requests

I have a chatbot system that creates a record in the table with user information during the first interaction. However, when the user sends multiple messages in a short period of time, my code cannot check if there is already an open session because it is still being processed. As a result, it creates multiple sessions instead of adding messages to the first one, causing duplicate records. I have already tried to use a middleware, but the problem still persists. I would like some help on how to solve this issue.

0 likes
1 reply
Snapey's avatar

have a look at firstOrCreate to get the existing chat or start a new one.

You need the operation to be atomic so that the find of an existing chat, and the creation of a new chat cannot be interrupted by a similar transaction.

1 like

Please or to participate in this conversation.