DEV Community

Michael Heap
Michael Heap

Posted on • Originally published at michaelheap.com on

Perform an action on team creation with Laravel Jetstream

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:

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

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:

php artisan make:listener CreateBaseCategories --event=TeamCreated
Enter fullscreen mode Exit fullscreen mode

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:

public function handle(TeamCreated $event)
{
  Category::createDefaultsForTeam($event->team->id);
}
Enter fullscreen mode Exit fullscreen mode

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:

// 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,
        ]
    ];
}
Enter fullscreen mode Exit fullscreen mode

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:

public function test_teams_can_be_created()
{
  Event::fake([
    TeamCreated::class,
  ]);

  // The rest of the test
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)