Streamlining Filament Forms with Namespace Updates and Tenant Context Resolution

Keeping dependencies up-to-date can be a headache, especially when upgrading major versions of frameworks. Let's look at how recent changes in the Reimpact platform addressed a namespace change in Filament v5 and improved tenant context resolution.

The Challenge: Filament v5 Namespace Update

Filament is a popular Rapid Application Development (RAD) framework for Laravel. A recent upgrade to Filament v5 introduced a namespace change that broke existing code in the GeneratedReportResource's create page. Specifically, the Filament\Forms\Get class was moved to Filament\Schemas\Components\Utilities\Get. This caused a TypeError because the old namespace was no longer valid.

The Solution: Updating the Namespace

The fix was straightforward: update all references to the Get class to use the new namespace. This ensured that the GeneratedReportResource's create page would function correctly with Filament v5.

use Filament\Schemas\Components\Utilities\Get;

// Example usage (formerly Filament\Forms\Get)
$component->afterStateHydrated(function (callable $set, Get $get) {
    $value = $get('some_other_component');
    $set('my_component', $value);
});

In this example, the Get class (now under the Filament\Schemas\Components\Utilities namespace) is used within a Filament form component to retrieve the state of another component. Updating the use statement resolves the TypeError.

Enhancing Tenant Context Resolution

In addition to the namespace update, the commit also included improvements to tenant context resolution for Livewire updates within the TenantResource. Specifically, it ensured that the auto-tenant-context is properly resolved. This is crucial in multi-tenant applications where each tenant needs to operate in isolation.

// Example of middleware simulating tenant context resolution
class EnsureTenantIsSet
{
    public function handle($request, Closure $next)
    {
        $tenantId = $request->header('X-Tenant-ID');

        if ($tenantId) {
            Tenant::setTenantId($tenantId);
        }

        return $next($request);
    }
}

This example illustrates the use of middleware to establish tenant context based on a request header. While this isn't the exact implementation within Filament, it demonstrates the core concept of dynamically setting the tenant based on the current request or session.

Conclusion

Upgrading dependencies can introduce breaking changes, but careful attention to detail and a methodical approach can minimize disruption. In this case, updating the namespace and improving tenant context resolution ensured that the Reimpact platform remained stable and functional after upgrading to Filament v5. Always review release notes and test thoroughly when upgrading dependencies to avoid unexpected issues.

Streamlining Filament Forms with Namespace Updates and Tenant Context Resolution
GERARDO RUIZ

GERARDO RUIZ

Author

Share: