F
Filament17mo ago
Nate

Switching Database Connection on a Resource

Hey filaments, I have a multi-tenancy database and I want to run actions on tenants. I have a TenancyResource and on the list page I was thinking about making that the entry point to manage tenants. So you click one of the tenants in the list and go to its view page and using relationship managers and some custom pages I though maybe there would be a good oprtunity to switch database context. Once you've clicked on a tenant I would have a list page with tabs to show user lists and and other stuff as well as actions to create records. What would be the best to achieve this. At what point, middleware wise or in the filament lifecycle can I switch DB context? Thanks ❤️
1 Reply
Nate
NateOP17mo ago
Hmm maybe adding a custom middleware in the config.
'middleware' => [
'auth' => [
Authenticate::class,
],
'base' => [
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
DispatchServingFilamentEvent::class,
MirrorConfigToSubpackages::class,
],
],
'middleware' => [
'auth' => [
Authenticate::class,
],
'base' => [
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
DispatchServingFilamentEvent::class,
MirrorConfigToSubpackages::class,
],
],
Found a way! Rgistered my new middleware:
// config/filament.php

'middleware' => [
'auth' => [
Authenticate::class,
],
'base' => [
InitializeTenancyBySubdomain:class,
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
DispatchServingFilamentEvent::class,
MirrorConfigToSubpackages::class,
],
],
// config/filament.php

'middleware' => [
'auth' => [
Authenticate::class,
],
'base' => [
InitializeTenancyBySubdomain:class,
EncryptCookies::class,
AddQueuedCookiesToResponse::class,
StartSession::class,
AuthenticateSession::class,
ShareErrorsFromSession::class,
VerifyCsrfToken::class,
SubstituteBindings::class,
DispatchServingFilamentEvent::class,
MirrorConfigToSubpackages::class,
],
],
//App\Http\InitializeTenancyBySubdomain

<?php

declare(strict_types=1);

namespace App\Http\Middleware;

use App\Context\Tenant\Models\Tenant;
use Illuminate\Http\Request;
use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain as BaseMiddleware;
use Closure;

class InitializeTenancyBySubdomain extends BaseMiddleware
{
public function handle(Request $request, Closure $next): mixed
{
$route = $request->route();
$record = $route->parameter('record');

if ($route->uri() === 'admin/tenancy/tenants/{record}' && $tenant = Tenant::find($record)) {
$subdomain = $tenant->domain->domain;
$request->headers->set('host', $subdomain . '.' . $request->getHost());
}

// Passes back to the tenancyforlaravel tenancy initialisation middleware.
return parent::handle(request: $request, next: $next);
}
}
//App\Http\InitializeTenancyBySubdomain

<?php

declare(strict_types=1);

namespace App\Http\Middleware;

use App\Context\Tenant\Models\Tenant;
use Illuminate\Http\Request;
use Stancl\Tenancy\Middleware\InitializeTenancyBySubdomain as BaseMiddleware;
use Closure;

class InitializeTenancyBySubdomain extends BaseMiddleware
{
public function handle(Request $request, Closure $next): mixed
{
$route = $request->route();
$record = $route->parameter('record');

if ($route->uri() === 'admin/tenancy/tenants/{record}' && $tenant = Tenant::find($record)) {
$subdomain = $tenant->domain->domain;
$request->headers->set('host', $subdomain . '.' . $request->getHost());
}

// Passes back to the tenancyforlaravel tenancy initialisation middleware.
return parent::handle(request: $request, next: $next);
}
}
Want results from more Discord servers?
Add your server