Geoff_S
Geoff_S

Reputation: 5105

Grabbing userID and field upon user creation

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);

Upvotes: 1

Views: 39

Answers (2)

Davit Zeynalyan
Davit Zeynalyan

Reputation: 8618

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);

Upvotes: 0

Tim Lewis
Tim Lewis

Reputation: 29288

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);
...

Upvotes: 6

Related Questions