Nick Pratley

Disabling Personal Teams in Laravel 8 and Jetstream

NickNick

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.

tl;dr

$ 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 create function.

Great, we create a user and now get no team, but also an error on login:

  • Facebook
  • Twitter
  • LinkedIn

Let’s overwrite some of the Traits in HasTeams to make these optional!

Create 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 app/Http/Kernel.php

Let’s add this teammiddleware to our dashboard routes now, in routes/web.php

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())

with

@if (Laravel\Jetstream\Jetstream::hasTeamFeatures() && Auth::user()->isMemberOfATeam())

Reloading the page now, we are greeted with the Create a Team form! Fantastic!

  • Facebook
  • Twitter
  • LinkedIn

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.

  • Facebook
  • Twitter
  • LinkedIn

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 🙂

https://npratley.net/building-laravel-8-for-production-release-with-horizon-and-octane-using-github-actions-c139e66f75de

Thanks for reading!t

Nick
Author

Comments 10
  • jeff
    Posted on

    jeff jeff

    Reply Author

    hey the image isn’t complete.


    • Nick
      Posted on

      Nick Nick

      Reply Author

      Looks like they broke when moving from Medium! All fixed!


    • Nick
      Posted on

      Nick Nick

      Reply Author

      Looks like they broke when moving from Medium! All fixed!


  • Nedder
    Posted on

    Nedder Nedder

    Reply Author

    Your links to the two files needed are broken. app/Traits/HasNoPersonalTeam.php


    • Nick
      Posted on

      Nick Nick

      Reply Author

      Looks like they broke when moving from Medium! All fixed! sorry about that!


  • Mårten Hansson
    Posted on

    Mårten Hansson Mårten Hansson

    Reply Author

    Great job! This is what I have been looking for. I find it strange that the createors missed this. It should be possible to set in the config.
    A suggestion: If a guest is not registered and clicks
    the Create account button, I don’t think it makes any sense to display the Create team form after registration. I mean the user is invited to a team and doesn’t yet know that creating their own team is possible. It might feel a bit confusing. Better to redirect to the dashboard. (Here, the user can add a new team at any time)


    • Nick
      Posted on

      Nick Nick

      Reply Author

      I’ve actually done that in a project now too – because 99% of the time a new user does not need to create a new account – they’ve been invited.

      On account create, if there is a pending invitation show that for acceptance, I f not show the new account form! This blog post was just the start of it 🙂


  • Mårten Hansson
    Posted on

    Mårten Hansson Mårten Hansson

    Reply Author

    On account create… could you be more specific? Maybe share some code.
    I am lost in RegisteredUserController
    🤞


Share This