How do I insert records into two related tables from a stored procedure in laravel? That is, register the user and then retrieve the id to insert information in another table.
How do I insert records into two related tables from a stored procedure in laravel? That is, register the user and then retrieve the id to insert information in another table.
It would be something similar to the following code:
$user = new User();
$user->name = "Paco"
$user->save(); // A partir de aqui User , ya tiene un id asociado
// Suponemos que usuarios --------> 1:N ---------> logs
// Por consiguiente desde User establecemos hasMany y desde Logs una belongsTo
$log = new Log();
$log->user->associate($user);
$log->save();
Once we save, automatically the object that we use to save, has already associated a new id
in the database, what we have to do in the new relation of the new object.
We have to notice that we can not save an object as logs
if in the database we do not have before user
since we have the problem that there is no id
that identifies it and above we have a problem in the consistency of the database.