Courses

[NEW] React Native Mobile App with Laravel 12 API

Simple Multitenancy Setup

You're reading a FREE PREVIEW of a PREMIUM course.
Summary of this lesson:
- Implement global scopes on Category and Transaction models to filter by `user_id`
- Modify controller create methods to associate new records with the authenticated user
- Add relationships in the User model to access associated categories and transactions

Link to the repository

[Only for premium members]

The last thing before we start working on the Flutter mobile application is that we want multi-tenancy for our Users. In our case, we want the easiest multi-tenancy option - user_id filtering. So, let's implement the following:

  • Global scopes for Category and Transaction models
  • Refactoring of Category and Transaction controllers to create records for our user

These are small changes, but they will make our application multi-tenant. Let's start with the global scope.


Global Scopes for Category and Transaction Models

Let's start with a quick overview of our logic:

  • On our Models, we have a user_id column that relates to the User
  • We want to apply a global scope to our Models to filter records by user_id
  • This should be done automatically when we query our Models

Let's implement this in our Category model...

The full lesson is only for Premium Members.
Want to access all 19 text lessons of this course? (91 min read)

You also get:

  • 77 courses
  • Premium tutorials
  • Access to repositories
  • Private Discord