0

I have two tables - users and user_attributes. During registration user I would like to not only enter the user in the user table but also create his attributes.

AuthController

    protected function create(array $data)
    {
        User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);

        return UserAttribute::create([
            'user_id' => $data['id'],  // Undefined index: id
            'lvl' => 0,
            'exp' => 0,
            'currency' => 500,
            'premium_currency' => 5,
        ]);
    }

How do I get user_id? PS: I'm sorry for my english.

2
  • 3
    the simplest way would be to do $user = User::create([...]). then UserAttribute::create(['user_id' => $user->id]); but you should really be using Eloquent relationships: laravel.com/docs/5.2/… Commented May 11, 2016 at 19:08
  • the data is correctly add to the database, but it shows the error, code Commented May 11, 2016 at 19:38

1 Answer 1

1

Do this $user =new User;
$user->thing1 = 12; $user->thing2 =32; $user->save();

And now as soon as you have called the save method you can get the id by doing

$user->id; 

Remember! It will only work after you have called the save method.

Also remember to replace my data with your data.

For further info and explanation see here: https://laravel.com/docs/5.2/eloquent#basic-inserts

Best of luck!

Sign up to request clarification or add additional context in comments.

3 Comments

the data is correctly add to the database, but it shows the error error code
I can with 100% safety say that this is not in anyway related to your insert. This is a mess up somewhere else im afraid i can't help you without seeing some more code pf various controllers and models :-)
I didn't return $user at the end :P

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.