Laravel 5.1: Register user and send data to two tables at the same time: Users and Users_details

2.8k views Asked by At

I need to register a new user and store data in two tables separately, the Users table will hold classic id /name / email / password information while the User_details table will hold different information related to the new registered user, for example gender, age, user_pic etc. Now, is there a way to modify the standard authcontroller and stuff from laravel 5.1 to accomplish that task? I mean, how do you tell the controller in the create function to use two different tables? AFAIK the authcontroller now uses User model to store the data in the DB but I need to use also the User_detail model because that is where I specify the fillable columns of my User_detail table. Any clue? Thanks

1

There are 1 answers

8
Sanoob On

Modify AuthController's create() function. If you have added relation for your model you can easily add this it somewhat it would be like this

  $user =User::create($data);
  $user_details = new User_Detail();
  $user_details->fill($data); 
  //If your have added fillable in User_Detail
  return $user_details->save($user);

This create function is called in RegistersUsers trait, For more information about inserting related models.