0
votes

When a user registers, how do I save some default user settings in a user_settings table I have table? I have the hasOne and belongsTo relation setup for the 2 models. I tried something like this in the register controller, but got an error.

    protected function create(array $data)
{

    $user = User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),
    ]);

    Return Usersetting::create([
        'user_id' => $user->id,
        'user_name' => 'u'.$user->id,
        'profile_name' => $user->name,
        'user_email' => $user->email,
        'user_private account' => 0,
        'user_fans_counter' => 1,
        'new_subscriber_alert' => 1,
        'new_tip_alert' => 1,
        'new_private_message_alert' => 1,
        'new_referral_alert' => 1,
    ]);
}
1
What was the error?ColinMD
SQL 'user_id' doesn't have a default valueShawn Sonnier

1 Answers

1
votes

You can strip the second part out and add a registered method in your RegisterController. If you have the relationships setup correctly then you can add like below

public function registered(Request $request, $user) 
{
    $user->userSetting()->create([
        'user_name' => 'u'.$user->id,
        'profile_name' => $user->name,
        'user_email' => $user->email,
        'user_private account' => 0,
        'user_fans_counter' => 1,
        'new_subscriber_alert' => 1,
        'new_tip_alert' => 1,
        'new_private_message_alert' => 1,
        'new_referral_alert' => 1,
    ]);
}

This way the user_id should automatically be populated through the relationship.