Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MARIN's avatar
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();
0 likes
0 replies

Please or to participate in this conversation.