0

In my app I have a users table and a user_profiles table. When creating a new user I need to fill the new users ID in the user_profile table.

Here is my store function in my users controller

public function store()
{
    $userData = new User(Input::only('username'));
    $userProfileData = new UserProfile(Input::except('username'));


    $userData->save();
    $userProfileData->save();

    Flash::success('A new user has been created successfully.');
    return Redirect::route('users.index');
}

How can I pass the newly created users id to the users_id column in the users_profile table?

1 Answer 1

1

Getting and assigning the ID might be easier than you think:

    public function store()
{
    $userData = new User(Input::only('username'));
    $userProfileData = new UserProfile(Input::except('username'));


    $userData->save();
    $userProfileData->users_id = $userData->id;
    $userProfileData->save();

    Flash::success('A new user has been created successfully.');
    return Redirect::route('users.index');
}

The id will be set in the $userData object after the execution of the save() method on the userData object. You can then assign it to the foreign key in user_profiles object.

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

1 Comment

You are right. I was way over thinking it. Thanks for that!

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.