Reputation: 55
i want to create a new record (row) in a different table in the data base. the user is created in this method
protected function create(array $data)
{
$confirmation_code = str_random(30);
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'confirmation_code' => $confirmation_code,
]);
}
I want to add the created user id in another table. for example "addresses" table.
I tried doing it inside the create method but I don't know the new user ID! how can I do this?
Upvotes: 1
Views: 868
Reputation: 1366
Try this :
protected function create(array $data)
{
$confirmation_code = str_random(30);
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'confirmation_code' => $confirmation_code,
]);
Address::create([
...
'user_id' => $user->id;
]);
return $user;
}
Upvotes: 2
Reputation: 1901
Try this:
$user = new User();
$user->name = $data['name'];
...
$user->save();
// get new user ID with
$id = $user->id;
Upvotes: 3