Jun 7, 2017
0
Level 2
Store input in multiple models
Hi,
For a small project I would like to import an Excel file into a database. I have 3 models; group, user and group_user (group/user association). The form contains 2 input fields; name (which corresponds to the group name) and a file upload (rows with users).
What is the best way to store the information and create the association? This approach will probably work, but it is the best way?
Create a new group
$group = new Group;
$group = $request->name;
$group->save();
Then open the Excel and loop through the rows and save the user
$user = new User;
$user->name = $row->name;
$user->save;
Within the loop after the user creation add the GroupUser association
$group_user = new GroupUser;
$group_user->group_id = $group->id;
$group_user->user_id = $user->id;
$group_user->save();
Please or to participate in this conversation.