Filament e Tenancyforlaravel - Multi-tenancy Multi-database #filament #laravel

3 min read 7 months ago
Published on Aug 20, 2025 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

In this tutorial, we will learn how to set up the Tenancyforlaravel package in a Filament v3 application. Multi-tenancy allows your Laravel application to manage multiple tenant databases, making it easier to handle different clients or user groups within a single application. This guide will provide you with step-by-step instructions to effectively implement this functionality.

Step 1: Install the Required Packages

To begin, you need to install the Tenancyforlaravel package. Follow these steps:

  1. Open your terminal.

  2. Navigate to your Laravel project directory.

  3. Run the following command to install the Tenancy package:

    composer require tenancy/tenancy
    
  4. After the installation, publish the configuration files using:

    php artisan vendor:publish --tag=tenancy-config
    

Step 2: Configure the Package

After installing the package, you need to configure it for your application.

  1. Open the config/tenancy.php file.
  2. Configure the database connection settings according to your requirements.
  3. Set up the tenant identification method. You can choose from subdomain, domain, or custom identification methods.

Step 3: Set Up Database Migrations

You will need to create migrations for your tenant databases.

  1. Create a migration using the following command:

    php artisan make:migration create_tenants_table
    
  2. Define the schema for the tenants in the migration file. For example:

    Schema::create('tenants', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('database');
        $table->timestamps();
    });
    
  3. Run the migration:

    php artisan migrate
    

Step 4: Create a Tenant

To create a new tenant, you need to run the following command:

php artisan tenancy:create

This will prompt you to enter the details for the new tenant, such as the tenant name and database name.

Step 5: Configure Filament

Now that your tenancy setup is complete, you need to integrate it with Filament.

  1. In your Filament configuration, ensure that the tenant database connection is used.
  2. Update your Filament resources to reflect tenant-specific data.

Step 6: Testing Your Setup

Once everything is configured:

  1. Run your Laravel application.
  2. Test the tenant creation process by accessing the tenant-specific routes.
  3. Ensure that each tenant can only access its own data.

Conclusion

You have successfully set up multi-tenancy in your Laravel application using the Tenancyforlaravel package and Filament v3. This setup allows for efficient management of multiple tenants, ensuring that each one has its own isolated database.

For next steps, consider exploring advanced features of the Tenancy package, such as tenant-specific migrations or customizing the tenant identification process. Happy coding!