简体   繁体   中英

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 ?

尝试使用->id返回对象的->id ,例如:

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

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

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!

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

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

$id = DB::table('users')->insertGetId(
    [ 'name' => 'John Doe', 'email' => 'john@example.com']
);

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;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM