1

I have this code to insert the information about the user in the database:

DB::table('users')->insert(
       array( 
        'first_name'   =>   $first_name,
        'last_name' => $last_name,
        'birth_date' => $birthDate,
        'email' => $email,
        'password' => $password,
        'profession' => $profession,
        'gender' => $gender,
        'age' => $age
    )
   );

It runs sweet and then I have the user redirected to a dashboard page. But, I need the id of the user to get info about him/her. So, How do I get the id returned after the insert statement has been done!

1 Answers1

0

use insertGetId();

$id = DB::table('users')->insertGetId(
   array( 
    'first_name'   =>   $first_name,
    'last_name' => $last_name,
    'birth_date' => $birthDate,
    'email' => $email,
    'password' => $password,
    'profession' => $profession,
    'gender' => $gender,
    'age' => $age
   )
);

or just laravel eloquent

$users = User::create([
'first_name'   =>   $first_name,
'last_name' => $last_name,
'birth_date' => $birthDate,
'email' => $email,
'password' => $password,
'profession' => $profession,
'gender' => $gender,
'age' => $age
]);

dd($user->id); // If you modified Id add into model $primaryKey = "modified_id" $user->modified_id;