Grabbing userID and field upon user creation

12
March 15, 2019, at 9:30 PM

I currently have a function that simply creates a user record in the database

$user->name;
$user->address;
$user->save();

Once this is done, I've created an id for them in my users table.

I know I can return $user, but specifically how can I grab the id and name from that to be used in a call like so:

$user->save();
//return $user id and $user name
$update = new UpdateTable($id,$name);
Answer 1

When you call $user->save(), the id (or primary key if it is not id) becomes available via $user->id. So, you'd simply pass the following to your UpdateTable() method:

$user = new User();
$user->name = "Test";
$user->address = "123 Somewhere";
$user->save();
$update = new UpdateTable($user->id, $user->name);
...
Answer 2

Try this code

$user = User::create(['name' => 'name', 'address' => 'Some address']);
$update = new UpdateTable($user->id, $user->name);

or

$update = new UpdateTable($user->getPrimaryKey(), $user->name);
READ ALSO
Submitting 2 Forms with 1 Button or 1 Input

Submitting 2 Forms with 1 Button or 1 Input

currently i have 2 Buttons (well 1 Button and 1 Input with a Bootstrap button class) I use to upload a picture, and add a text to that picutre

9
How to optimize a mysql query

How to optimize a mysql query

I have a problem with a mysql query that sometimes needs more than 80 secs to execute

42
Angular 7 : Cannot add or update a child row: a foreign key constraint fails

Angular 7 : Cannot add or update a child row: a foreign key constraint fails

I am stuck on this SQL error and I do not understand why:

33