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

MichaelDaly's avatar

Alter registration logic when creating teams

Hi,

I am building a project with Spark and I am wanting to automatically generate a slug based on a Team name when a new user registers.

This seems like a really trivial piece of functionality to add, however, I am struggling to figure out where I should alter the registration code and what method I need to override to include the slug generation during registration.

This is what I have done so far:

  • Added a migration script to add a new 'slug' field to the 'Teams' table
  • Located the following method which I believe I should be able to extend: spark/src/Repositories/TeamRepository@create
  • Added the following override script to my SparkServiceProvider:
Spark::swap('TeamRepository@create', function ($user, array $data) {
    return Spark::team()->forceCreate([
            'owner_id' => $user->id,
            'name' => $data['name'],
        'slug' => 'add-a-test-for-now',
            'trial_ends_at' => Carbon::now()->addDays(Spark::teamTrialDays()),
        ]);
});

This code does not override the create team functionality during registration and therefore is not adding the slug.

I have thought about using event listeners and firing some logic to generate a slug after a team has been successfully created, however, this is not an ideal solution as I want the slugs to be unique. Ideally, I would be able to intercept the registration logic and allow users to alter the team name if the unique validation method fails.

I am hoping someone has been able to modify the registration methods in a similar way and can help to point me in the right direction.

Thanks in advance.

0 likes
1 reply
MichaelDaly's avatar

Ah, I am sorry, I was too eager with my question.

After spending a little longer investigating the issue, I discovered I had not included a reference to 'Carbon' in my SparkServiceProvider which in turn was causing an exception.

I have included the reference and now the create team override is working as expected.

1 like

Please or to participate in this conversation.