In this episode, we're diving into building the foundational Team model and establishing the relationship between users and teams. First up, we create the basic Team model and hook it up with a simple migration—just a team name for now, but you can easily extend this with more fields later if you need.
Next, we set up the 'many-to-many' relationship between users and teams. We do this with a new pivot table called team_user
and make sure our foreign keys are nicely constrained. After running our migrations, we connect everything up in the User model using the belongsToMany
relationship—a classic Laravel move.
With our models wired, we then write a feature test to make sure users can join teams. We use Factories to quickly whip up fake users and teams, attach them, and verify in the test that the connection works as expected. It's a great way to double-check everything before moving on.
Finally, we do a quick check in the browser by dumping out the authenticated user's teams on the dashboard. Once you manually add a team or two to your user in the database, you'll see it pop up, confirming everything's set up nicely. So by the end of this episode, you've got the groundwork in place for users to join teams—ready to expand from here!