Ensuring Tenant Context for Filament Uploads in Laravel

Introduction

When building multi-tenant applications with Laravel and Filament, it's crucial to ensure that operations are performed within the correct tenant context. This post addresses a common issue encountered when using Filament's massive upload feature where the tenant context was not active during Livewire form submissions, leading to data integrity violations.

The Problem: Tenant Context Inconsistency

Imagine you're building a platform where multiple companies (tenants) use the same application instance but have isolated data. Filament's CreateMassiveUpload page, designed for handling large file uploads, was failing because the company_id (representing the tenant) was not being correctly associated with the uploaded data. This occurred because the TenantContext middleware was not active during the Livewire form submission process within the Filament page. Consequently, the system attempted to insert data into the wrong table, triggering a NOT NULL constraint violation on the company_id column.

The Solution: Activating Tenant Context

To resolve this, the tenant context needs to be explicitly activated during the Livewire form submission. This ensures that any database operations performed during the upload process are executed within the scope of the correct tenant. This can be achieved by ensuring the appropriate middleware is applied to the Filament route or Livewire component.

Consider the following example demonstrating how tenant context could be managed using middleware:

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use App\Models\Tenant;

class EnsureTenantContext
{
    public function handle(Request $request, Closure $next)
    {
        $tenant = Tenant::find($request->header('X-Tenant-ID'));

        if ($tenant) {
            app()->instance('tenant', $tenant);
        }

        return $next($request);
    }
}

In this illustrative middleware, the tenant is resolved based on a header, and then bound to the application container.

The Fix: Dispatching the Processing Job

In addition to the tenant context issue, the post also highlights that the processing job (StoreMassiveUpload), responsible for handling the uploaded data, was not being dispatched after the upload was complete. This meant that the uploaded files were sitting idle without being processed and stored correctly.

The fix involved explicitly dispatching the StoreMassiveUpload job after the massive upload creation. This ensures that the uploaded data is processed in the background, allowing the user to continue working without waiting for the upload to complete.

use App\Jobs\StoreMassiveUpload;

// After the massive upload is created
StoreMassiveUpload::dispatch($massiveUpload);

Results

By ensuring the tenant context is active during Livewire form submissions and dispatching the processing job, the Filament massive upload feature now functions correctly in a multi-tenant environment. This prevents data integrity violations and ensures that uploaded files are processed and stored efficiently.

Next Steps

Review your Filament form submissions and ensure that tenant context is correctly established, especially when dealing with multi-tenant applications. Verify that background jobs are dispatched appropriately to handle asynchronous tasks, such as processing large file uploads. Consider implementing request validation to check that tenant IDs are valid and exist within your application.

Ensuring Tenant Context for Filament Uploads in Laravel
GERARDO RUIZ

GERARDO RUIZ

Author

Share: