Skip to content

Instantly share code, notes, and snippets.

@pratikkuikel
Created November 22, 2025 14:01
Show Gist options
  • Select an option

  • Save pratikkuikel/3ee4e76f14576de80bcc78bbcabfeba5 to your computer and use it in GitHub Desktop.

Select an option

Save pratikkuikel/3ee4e76f14576de80bcc78bbcabfeba5 to your computer and use it in GitHub Desktop.
How to bind tenant user model to filamentPhp's export when using stancl's tenancyforlaravel ?
<?php
namespace App\Bootstrappers;
use App\Models\Tenant\User as TenantUser;
use App\Models\User;
use Illuminate\Contracts\Auth\Authenticatable;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant;
// Auth bootstrapper that runs when tenant is initialized
class AuthBootstrapper implements TenancyBootstrapper
{
public function bootstrap(Tenant $tenant)
{
config(['auth.providers.users.model' => TenantUser::class]);
app()->bind(Authenticatable::class, function () {
return app(TenantUser::class);
});
}
public function revert(): void
{
config(['auth.providers.users.model' => User::class]);
app()->bind(Authenticatable::class, function () {
return app(User::class);
});
}
}
<?php
namespace App\Overrides;
use Filament\Actions\Exports\Enums\Contracts\ExportFormat as ExportFormatInterface;
use Filament\Actions\Exports\Enums\ExportFormat;
use Filament\Actions\Exports\Models\Export;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Symfony\Component\HttpFoundation\StreamedResponse;
use function Filament\authorize;
// custom DownloadExport class that overrides Filament's default DownloadExport class
class DownloadExport
{
public function __invoke(Request $request, Export $export): StreamedResponse
{
if (filled(Gate::getPolicyFor($export::class))) {
authorize('view', $export);
} else {
abort_unless($export->user()->is(auth('tenant')->user()), 403);
}
$format = $this->resolveFormatFromRequest($request);
abort_unless($format !== null, 404);
return $format->getDownloader()($export);
}
protected function resolveFormatFromRequest(Request $request): ?ExportFormatInterface
{
return ExportFormat::tryFrom($request->query('format'));
}
}
<?php
/**
* Tenancy bootstrappers are executed when tenancy is initialized.
* Their responsibility is making Laravel features tenant-aware.
*
* To configure their behavior, see the config keys below.
*/
'bootstrappers' => [
Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper::class,
Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper::class,
Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper::class,
Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper::class,
AuthBootstrapper::class, // custom authbootstrapper that changes the user model for tenant
CurrentBusinessBootstrapper::class,
SpatiePermissionsBootstrapper::class,
Vidwan\TenantBuckets\Bootstrappers\TenantBucketBootstrapper::class,
// Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper::class, // Note: phpredis is needed
],
<?php
use App\Overrides\DownloadExport;
// Placed at last of tenant route file
Route::get('/filament/exports/{export}/download', DownloadExport::class)
->name('filament.exports.download')
->middleware([
'web',
InitializeTenancyByDomainOrSubdomain::class,
PreventAccessFromCentralDomains::class,
FileUrlMiddleware::class,
CheckTenantForMaintenanceMode::class
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment