TheGod39
TheGod39

Reputation: 553

Laravel: Get the ID of User::create and insert new row using that ID

I have AuthController in Laravel and I have 2 tables, one is Users and one is Users_Information and I want to insert into Users_Information upon registration.

So I want to get the id from the following method and insert a new row and set the column ID of that row to the ID of the user I have just created.

     /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'username' => $data['username'] . ' ' . $data['username2'],
            'mail' => $data['mail'],
            'password' => bcrypt($data['password']),
        ]);
    }

I want to insert into Users_Information with a column id, current_food and current_level

I have a controller for the Users_Information called UserInformation, would I just call UserInformation::create but how would I get the id from the User::create?

Upvotes: 52

Views: 140240

Answers (7)

Aoryu
Aoryu

Reputation: 41

Remember that if you set your table with an custom ID, you must indicate so in the code. In my case, my table "Antecedentes" has the custom id "ant_id" so "id" did not work and had to put "ant_id" like this:

$success = Antecedentes::create($data)->ant_id;

And i could go on.

Upvotes: 0

Md Shohag
Md Shohag

Reputation: 119

Suppose, I have a model name Employee and I want to insert some data in this model also want to get table id. So I can achieve this easily by below code:

 $employee = new Employee();
 $employee->employeeName = 'Something';
 $employee->save();
 $employee->id;

Upvotes: 11

Abid Shah
Abid Shah

Reputation: 475

use insertGetId(); it gives you the id of an inserted row.

$userId = User::insertGetId([
    'name' => $request->input('name'),
     'email' => $request->input('email'),
     'password' => bcrypt($request->input('password')),
]);

https://laravel.com/docs/5.7/queries#inserts

Upvotes: 14

Felippe Duarte
Felippe Duarte

Reputation: 15131

Also, if you are not using Eloquent, you can use insertGetId:

$id = DB::table('users')->insertGetId(
    [ 'name' => 'John Doe', 'email' => '[email protected]']
);

Upvotes: 6

camelCase
camelCase

Reputation: 5608

Eloquent has a nice way to handle saving relationships, which can be used in your case. It allows you to save a related model without accessing the model directly. Of course you must make sure your relationship is defined in the appropriate models first.

Below will create the user and their information. I assumed the method of your relationship was called information but you can adjust as needed.

$user = User::create([
    'username' => $data['username'] . ' ' . $data['username2'],
    'mail' => $data['mail'],
    'password' => bcrypt($data['password']),
])->information()->create([
    'current_food' => $current_food,
    'current_level' => $current_level
]);

Notice that we did not explicitly set user_id because we simply created the information by accessing the relationship you have defined; Laravel/Eloquent handles that for you!

Upvotes: 8

Pawel Bieszczad
Pawel Bieszczad

Reputation: 13335

The create() method returns the model.

$user = User::create([
    'username' => $data['username'] . ' ' . $data['username2'],
    'mail' => $data['mail'],
    'password' => bcrypt($data['password']),
]);

$userInfo = UserInformation::create([
    'user_id' => $user->id,
    'current_food' => $food,
    'current_level' => $level,
]);

Upvotes: 54

Alexey Mezenin
Alexey Mezenin

Reputation: 163978

Try to use ->id of returned object, something like:

$id = $this->create($data)->id;

Upvotes: 87

Related Questions