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

kylehudson's avatar

Fire event after team register

Hi guys,

I am new to all this Spark how do I fire an event after registration, like sending a confirmation email?

Sorry if this is a simple question, can't find anything in the L5 / Spark docs.

0 likes
5 replies
martinbean's avatar

@kylehudson There already is an event fired when a team is created: Laravel\Spark\Events\Teams\TeamCreated. You just need to bind a listener to it in your application’s EventServiceProvider class:

protected $listen = [
    Laravel\Spark\Events\Teams\TeamCreated::class => [
        App\Listeners\YourListenerName::class,
    ],
];
garethdaine's avatar

Just to add to the answer by @martinbean, go to your app\Providers\EventServiceProvider.php file and add your listener to the $listen property, specifically the Laravel\Spark\Events\Teams\Subscription\TeamSubscribed event:

'Laravel\Spark\Events\Teams\Subscription\TeamSubscribed' => [
    'Namespace\Listeners\Teams\Subscription\SendWelcomeEmail',
],

Then in your console use the php artisan event:generate command to generate your listener. It will be located at Namespace\Listeners\Teams\Subscription\SendWelcomeEmail, where Namespace is your apps primary namespace.

Then you just need to add your mail code to the handle method of the listener. Something like this for example:

public function handle(TeamSubscribed $event)
{
    $user = $event->user;

    Mail::send('emails.team.subscription.welcome', ['user' => $user], function($message) use ($user) {
        $message->from('[email protected]', 'Company Name');
        $message->to($user->email, $user->name)->subject('Welcome '.explode(' ',trim($user->name))[0]);
    });
}

Hope this helps.

1 like
jhull's avatar

Followed the above exactly to send an API call to email list app, and with Spark I'm getting an error message after the User tries to register (contact provider, the red modal that pops up).

Funny thing is, if I drop the event into a controller it works great:

        $user = $event->user;
        $api_key = '<secret>';

        $client = new Client();
        $url = "https://api.emailprovider.com/subscribe";
        $data = ['email' => $user->email, 'name' => $user->name, 'api_key' => $api_key];

        $response = $client->post($url, ['query' => $data]);

This works great in a controller, but as part of an event listener it's giving me that error in the Register window - with no extra info to try and figure out why.

I have HookRegisteredUser in App\Listeners that looks like the above with this wrapping it:

    public function handle(UserRegistered $event)
    {

and then in my App\Providers\EventServiceProviders.php I have this:

       'Laravel\Spark\Events\Auth\UserRegistered' => [
            'Laravel\Spark\Listeners\Subscription\CreateTrialEndingNotification',
            'App\Listeners\HookRegisteredUser',
        ],

Any ideas where the Error is coming from - in the Console I'm just seeing 500.

Please or to participate in this conversation.