I have a form in which a user can publish a project in a database along with creating a new user in which both are connected to each other.
The usersid and projects.user_id values โโin the table. But I have a little problem. If I get the last identifier from the users table and use this +1 for the next id, it works. but if the user is deleted before adding a new project with an identifier, he can become really confused when starting the application.
let's say I got the last user as id 5 in the table. but someone deleted it, and I did a new project with a new user the next day. Now the next identifier will be id 6. but according to my code it reflects 5 in project_table, but in user_table it becomes 6.
I was wondering if anyone could suggest me a function that I could use for the correct identifier?
to get the last id
public static function getLastRow(){ $data = DB::table('users')->select('id') ->orderBy('id', 'desc') ->first(); return $data->id; }
before the validator, I count the last identifier to add it to projects
$lastidplus = (int)User::getLastRow() + 1;
in my validator I have this:
'user_id' => $lastidplus,
NOTE :: this works, but if in the future the last row in the user table will be deleted. a chain reaction of false identifiers will appear in the corresponding table of projects, since it will always have 1 difference. and I want to make this impossible.
source share