FilamentPHP can be integrated with multi-tenancy setups, allowing you to manage resources and admin panels for multiple tenants. Here's how you can use Filament with a multi-tenancy package like Spatie's Laravel Multi-Tenancy or Tenancy for Laravel:
1. Understanding Multi-Tenancy in Filament
Multi-tenancy allows you to serve different groups of users (tenants) using the same application, often with separate databases or table scopes. Filament works seamlessly with multi-tenancy by configuring resources, middlewares, and tenant-aware models.
2. Setting Up Tenancy
Using Spatie's Laravel Multi-Tenancy:
Install the package:
bash
نسخ الكود
composer require spatie/laravel-multitenancy
Publish the configuration:
bash
نسخ الكود
php artisan vendor:publish –tag="multitenancy-config"
Configure tenant resolution (e.g., based on subdomains, domains, or user attributes).
3. Making Filament Tenant-Aware
A. Middleware for Tenant Context
Ensure tenant resolution is applied to Filament routes. Add the tenant middleware to Filament's group in your filament.php configuration file:
php
نسخ الكود
'middleware' => [
'web',
'auth',
\Spatie\Multitenancy\Middleware\TenantMiddleware::class,
],
This ensures that the tenant is resolved for any request to the Filament admin panel.
B. Tenant-Aware Models
Use tenant-aware models to scope resources to the current tenant. For example, if your application uses a Post model, it should belong to a tenant.
Add the BelongsToTenant trait to your models:
php
نسخ الكود
use Spatie\Multitenancy\Models\Concerns\UsesTenantConnection;
class Post extends Model
{
use UsesTenantConnection;
protected $fillable = ['title', 'content'];
}
Alternatively, if you're using Tenancy for Laravel, use their BelongsToTenant trait or tenant_id column scoping.
C. Configuring Filament Resources
In your Filament resources, ensure tenant-specific data is loaded. Modify the query in the resource:
php
نسخ الكود
use Illuminate\Database\Eloquent\Builder;