I’ve started building a new portal for work, after many iterations we have settled on Laravel 8 and Jetstream.
The biggest annoyance so far has been a personal team getting created for every single user. We don’t want these unused entries clogging up the database, as we will consider a team as a company that we transact with.
Removing them is quite easy, so let’s dive right in.
First, let’s assume we’ve bootstrapped the Laravel installation and installed Jetstream.
$ curl -s “https://laravel.build/laravel-boilerplate" | bash
$ cd laravel-boilerplate
$ sail up -d
$ sail composer require laravel/jetstream
$ sail artisan jetstream:install livewire --teams
$ sail npm install
$ sail npm run dev
$ sail artisan migrate
$ sail artisan vendor:publish --tag=jetstream-views
Sweet, we’ve now got a working stack that will create teams for each user. Let’s fix that.
We need to edit
app/Actions/Fortify/CreateNewUser.php and comment out
$this->createTeam($user); from the
Great, we create a user and now get no team, but also an error on login:
Let’s overwrite some of the Traits in HasTeams to make these optional!
app/Traits/HasNoPersonalTeam.php with the following contents:
We now need to update the User model
app/Models/User.php and overwrite two methods on the Jetstream HasTeams trait.
We’ll now create a new Middleware to verify that the user has a team and redirect to the jetstream create team page if not. This also ensures a valid team is selected once created. Create
app/Http/Middleware/EnsureHasTeam.php with the following contents.
Now register this in
Let’s add this
teammiddleware to our dashboard routes now, in
And finally, we need to add one more condition to the
navigation-menu.blade.php file to only show the teams dropdown if the user is a member of any team. In
resources/views/navigation-menu.blade.php we want to replace both instances of
@if (Laravel\Jetstream\Jetstream::hasTeamFeatures() && Auth::user()->isMemberOfATeam())
Reloading the page now, we are greeted with the Create a Team form! Fantastic!
Creating a team now will redirect back to the dashboard once done, and the middleware will no longer force a new team to be created.
This means that anyone with an invite email can now register and accept without creating unnecessary teams! 🙂
Check the full code here, https://github.com/DevLAN-io/laravel-boilerplate and a diff of all changes here: https://github.com/DevLAN-io/laravel-boilerplate/commit/ad38d105380cc797c987f44a80ee122a63fac0f9
Looking to deploy this to production? Get started by building the containers 🙂
Thanks for reading!t