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

nicks's avatar
Level 3

Adding new registration fields to Team

I need to add additional fields to both User and Team. The Cookbook in the Spark Documentation describes how to add fields to User, and it works great. I felt that obvious thing to do is to put a Spark::createTeamsWith() method in SparkServiceProvider.php. This is definitely being called, but it is not passed the new fields - these only go to createUsersWith().

Is this the correct way to create additional fields in Team? And how do I get the data into createTeamsWith()?

The methods I have are:

        Spark::createUsersWith(function ($request) {
            Log::debug('createUsersWith($request=' . json_encode($request) . ')');

            $user = Spark::user();

            $data = $request->all();
            Log::debug('$data=' . json_encode($data));

            $user->forceFill([
                'name'                          => $data['name'],
                'email'                         => $data['email'],
                'password'                      => Hash::make($data['password']),
                'last_read_announcements_at'    => Carbon::now(),
                'phone'                         => $data['phone'],
                'marketing'                     => $data['marketing'],              
            ])->save();

            return $user;
        });  

        Spark::createTeamsWith(function ($user, array $data) {
            Log::debug('createTeamsWith($user=' . json_encode($user) . ', $data=' . json_encode($data) . ')');

            $attributes = [
                'owner_id'              => $user->id,
                'name'                  => $data['name'],
                'trial_ends_at'         => Carbon::now()->addDays(Spark::teamTrialDays()),
                'venue_address_line_1'  => $data['venue_address_line_1'],
                'venue_address_line_2'  => $data['venue_address_line_2'],
                'venue_town'            => $data['venue_town'],
                'venue_county'          => $data['venue_county'],
                'venue_postcode'        => $data['venue_postcode'],
                'venue_country'         => $data['venue_country'],
                'venue_type'            => $data['venue_type'],
                'venue_type_other'      => ($data['venue_type']=='Other') ? $data['venue_type_other'] : null,
            ];

            if (Spark::teamsIdentifiedByPath()) {
                $attributes['slug'] = $data['slug'];
            }

            return Spark::team()->forceCreate($attributes);
            });         

When createUsersWith() is called, all the new fields for both the user and the team are passed within $request. When createTeamsWith() is called, only name and slug are passed in $data[].

0 likes
2 replies
nicks's avatar
nicks
OP
Best Answer
Level 3

Eventually I found the answer at https://laracasts.com/discuss/channels/spark/adding-registration-fields-for-teams, though I can update this for Spark v9. It seems that you need to add the following function in the booted() method of SparkServiceProvider.

Spark::swap('Register@configureTeamForNewUser', 
    function (RegisterRequest $request, $user)
    {
        if ($invitation = $request->invitation()) {
            Spark::interact(AddTeamMember::class, [$invitation->team, $user, $invitation->role]);

            self::$team = $invitation->team;

            $invitation->delete();
        } elseif (Spark::onlyTeamPlans()) {
            list(self::$team, $paymentId) = Spark::interact(CreateTeam::class, [
                $user, [
                    'name'                  => $request->team, 
                    'slug'                  => $request->team_slug,
                    'venue_address_line_1'  => $request->venue_address_line_1,
                    'venue_address_line_2'  => $request->venue_address_line_2,
                    'venue_town'            => $request->venue_town,
                    'venue_county'          => $request->venue_county,
                    'venue_postcode'        => $request->venue_postcode,
                    'venue_country'         => $request->venue_country,
                    'venue_type'            => $request->venue_type,
                    'venue_type_other'      => $request->venue_type_other,
                ]
            ]);
        }

        $user->currentTeam();
    });
nicks's avatar
Level 3

One other finding. You can't use Spark::createTeamsWith() as this does not create the required record in the team_users table or set the users.current_team_id field, resulting in errors down the line. The approach which seems to work is to swap TeamRepository@create.

        Spark::swap('TeamRepository@create',
            function($user, array $data){
                $attributes = [
                    'owner_id'              => $user->id,
                    'name'                  => $data['name'],
                    'trial_ends_at'         => Carbon::now()->addDays(Spark::teamTrialDays()),
                    'venue_address_line_1'  => $data['venue_address_line_1'],
                    'venue_address_line_2'  => $data['venue_address_line_2'],
                    'venue_town'            => $data['venue_town'],
                    'venue_county'          => $data['venue_county'],
                    'venue_postcode'        => $data['venue_postcode'],
                    'venue_country'         => $data['venue_country'],
                    'venue_type'            => $data['venue_type'],
                    'venue_type_other'      => ($data['venue_type']=='Other') ? $data['venue_type_other'] : null,
                ];

                if (Spark::teamsIdentifiedByPath()) {
                    $attributes['slug'] = $data['slug'];
                }

                return Spark::team()->forceCreate($attributes);

            });

Please or to participate in this conversation.