Skip to content

Instantly share code, notes, and snippets.

@neverything
Last active December 16, 2023 14:14
Show Gist options
  • Save neverything/b21b4affc28a25b706c134d1d22f9910 to your computer and use it in GitHub Desktop.
Save neverything/b21b4affc28a25b706c134d1d22f9910 to your computer and use it in GitHub Desktop.
Handle free users with Filament and Laravel Spark using a custom Middleware. Learn more: https://silvanhagen.com/free-users-filament-laravel-spark/
<?php
// namespace and more...
class AppServiceProvider extends ServiceProvider
{
public function boot(): void
{
// Bind our implementation
$this->app->bind(
'Filament\Billing\Providers\Http\Middleware\VerifySparkBillableIsSubscribed',
VerifyProjectIsBillable::class
);
}
}
<?php
// namespace and more ...
class FeedbackResource extends Resource
{
// rest of the class...
public static function isTenantSubscriptionRequired(Panel $panel): bool
{
return true;
}
}
<?php
// namespace and use...
class Project extends Model implements HasCurrentTenantLabel
{
use Billable, HasFactory;
// rest of the class...
public function isBillable(): bool
{
return ! $this->free_forever;
}
public function isOnTrialOrSubscribed(): bool
{
return $this->onTrial() || $this->subscribed();
}
}
<?php
namespace App\Http\Middleware;
use Filament\Billing\Providers\Http\Middleware\VerifySparkBillableIsSubscribed;
use Filament\Facades\Filament;
class VerifyProjectIsBillable extends VerifySparkBillableIsSubscribed
{
public function handle($request, $next, $subscription = 'default', $plan = null)
{
$project = Filament::getTenant();
if ($this->shouldRedirectToBilling($project, $request)) {
return parent::handle($request, $next, $subscription, $plan);
}
return $next($request);
}
protected function shouldRedirectToBilling($project, $request): bool
{
return $project
&& $project->isBillable()
&& ! $project->isOnTrialOrSubscribed();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment