Answer the question
In order to leave comments, you need to log in
How to block concurrent editing of record in db on laravel?
Good afternoon! How to implement a ban on simultaneous editing of a record in a MySQL database using LARAVEL? That is, if one user edits a record, then another should be given a message that the record is currently locked. It is also necessary to provide for the situation that the user will not save the changes, but simply close the browser. Then the lock must also be removed! Admins can work both on a PC and on a mobile device (and android browsers do not track tab closing).
Answer the question
In order to leave comments, you need to log in
you can try to do without blocking if the situation is not very frequent.
give several users the opportunity to get a record for modification, while adding the field TimeChange to the table - where to enter the time of the last modification of the row. when making changes to the database, check the key of the updated record and the modification time (update .... where id=1 and lastEdit='lasteditdate'). if the modification time does not match, then inform the user that the record he is editing has already been changed by someone and show him the new changes.
After reading the comments and answers, I realized that I do not know and do not know how to use a websocket. And I made this option. The database has a locked_at field, where the editing time is stored, and a locked_by field, where the user id is stored. Accordingly, if other users try to edit or delete a record, then there is a check in the database; if the current time is more than 1 minute of time in locked_at, then this can be done. And as long as the user edits the record, there is an ajax request every 30s, which updates the locked_at field. And accordingly, when the tab is closed or saved, then ajax does not fly and after 1 minute the first one to reach will get the opportunity to edit.
There are two ideologically different implementation options:
1. The main one is the one who first opened the editing form (you described this option above). Requires additional fields like locked_at, ajax, etc.
2. The main one is the one who first changed. Two users open the form, edit. The first saves without problems, and the second , when saving , receives a message that the record has already been changed earlier. You can show him diff or something else ... (this option was described by Vladimir S). There are no extras here. fields are not needed, just check before saving that the updated_at field has not changed.
How to implement a ban on simultaneous editing of a record in a MySQL database using LARAVEL?Add a flag (a column in the database), a similar functionality was invented and has been used since the shaggy years, respectively. when trying to start editing a post, this flag must be checked.
And in android browsers, the closing of the tab is not tracked !!!I'll tell you a secret, closing a tab even in browsers "not android" can not always be tracked and I would not completely rely on something like that. One of the options for "reliable" tracking of the presence of a "client on the line" looks something like this:
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question