Perform an action on team creation with Laravel Jetstream

12 Jan 2021 in Development

In my latest Laravel Jetstream project there are some configuration options that are set on a per-team basis. Initially I edited the Team model to add a static::created method that directly called my Category model:

php
public static function boot() {
parent::boot();
static::created(function (Team $team) {
// createDefaultsForTeam is a custom method that I added
Category::createDefaultsForTeam($team->id);
});
}

As I started adding more side effects to Team creation I realised that this boot method was too long and knew too much about the rest of my application.

Fortunately, Jetstream emits a TeamCreated event that we can hook into and perform any actions we need in response to a team being created.

You can create a listener for this event with the following command:

bash
php artisan make:listener CreateBaseCategories --event=TeamCreated

Once that’s created, open up app/Listeners/CreateBaseCategories.php and make sure to add use Laravel\Jetstream\Events\TeamCreated; to import the event to listen to.

The handle method will be empty, so let’s call Category:: createDefaultsForTeam in there:

php
public function handle(TeamCreated $event)
{
Category::createDefaultsForTeam($event->team->id);
}

The team that was created is accessible as $event->team.

Now that our event listener is created, we need to update app/Providers/EventServiceProvider.php to add a new listener to the $listen array that will trigger when a team is created:

php
// Make sure to import the TeamCreated event
use Laravel\Jetstream\Events\TeamCreated;
class EventServiceProvider extends ServiceProvider
{
protected $listen = [
// ...other listeners...
TeamCreated::class => [
\App\Listeners\CreateBaseCategories::class,
]
];
}

Now, whenever a team is created our CreateBaseCategories listener will be called and the configuration options for that team will also be created.

This has an added benefit that you can disable side effects in your tests by disabling the event from firing:

php
public function test_teams_can_be_created()
{
Event::fake([
TeamCreated::class,
]);
// The rest of the test
}