Don't want Create Form to show if No Subscription

I want to show No Subscription notification without showing Create Form when user clicks on Create button in List Page. I have this in create page. I also tried beforeFill but halt gives error.
protected function handleRecordCreation(array $data): Model
{
$data['reference'] = RandomStringGeneratorService::generateRandomString();
$data['member_id'] = Auth::user()->member->id;

if ($data['is_published'] === true){
$data['published_by'] = Auth::id();
$data['published_at'] = now();
}
else{
$data['published_by'] = null;
$data['published_at'] = null;
}
// Unset the 'is_published' field
unset($data['is_published']);

return static::getModel()::create($data);
}

protected function beforeCreate(): void
{
$subscriptionService= app(SubscriptionService::class);
if (! $subscriptionService->canPerformAction(auth()->user()->member, ModelType::JOB_SEEKING_POST, 'create')) {
Notification::make()
->warning()
->title('You don\'t have an active subscription!')
->body('Choose a plan to continue.')
->persistent()
->actions([
Action::make('subscribe')
->button()
->url(route('subscribe'), shouldOpenInNewTab: true),
])
->send();

$this->halt();
}
}
protected function handleRecordCreation(array $data): Model
{
$data['reference'] = RandomStringGeneratorService::generateRandomString();
$data['member_id'] = Auth::user()->member->id;

if ($data['is_published'] === true){
$data['published_by'] = Auth::id();
$data['published_at'] = now();
}
else{
$data['published_by'] = null;
$data['published_at'] = null;
}
// Unset the 'is_published' field
unset($data['is_published']);

return static::getModel()::create($data);
}

protected function beforeCreate(): void
{
$subscriptionService= app(SubscriptionService::class);
if (! $subscriptionService->canPerformAction(auth()->user()->member, ModelType::JOB_SEEKING_POST, 'create')) {
Notification::make()
->warning()
->title('You don\'t have an active subscription!')
->body('Choose a plan to continue.')
->persistent()
->actions([
Action::make('subscribe')
->button()
->url(route('subscribe'), shouldOpenInNewTab: true),
])
->send();

$this->halt();
}
}
Solution:
I created custom create action listpage. ``` protected function getHeaderActions(): array { return [ Actions\Action::make('create') ->label('Create Job Seeking Post')...
Jump to solution
1 Reply
Solution
Pritbor
Pritbor2mo ago
I created custom create action listpage.
protected function getHeaderActions(): array
{
return [
Actions\Action::make('create')
->label('Create Job Seeking Post')
->action(function () {
// This action will be called when the user clicks "Create"
// It will first check for subscription status.
$this->checkSubscriptionAndRedirect();
}),
];
}

protected function checkSubscriptionAndRedirect()
{
$subscriptionService = app(SubscriptionService::class); // Get your subscription service

if (! $subscriptionService->canPerformAction(Auth::user()->member, ModelType::JOB_SEEKING_POST, 'create')) {
Notification::make()
->warning()
->title('You don\'t have an active subscription!')
->body('Choose a plan to continue.')
->persistent()
->actions([
Action::make('subscribe')
->button()
->url(route('subscribe'), shouldOpenInNewTab: true),
])
->send();

return; // Stop further execution
}

// If subscribed, redirect to create page
return redirect()->route('filament.user.resources.job-seeking-posts.create');
}
protected function getHeaderActions(): array
{
return [
Actions\Action::make('create')
->label('Create Job Seeking Post')
->action(function () {
// This action will be called when the user clicks "Create"
// It will first check for subscription status.
$this->checkSubscriptionAndRedirect();
}),
];
}

protected function checkSubscriptionAndRedirect()
{
$subscriptionService = app(SubscriptionService::class); // Get your subscription service

if (! $subscriptionService->canPerformAction(Auth::user()->member, ModelType::JOB_SEEKING_POST, 'create')) {
Notification::make()
->warning()
->title('You don\'t have an active subscription!')
->body('Choose a plan to continue.')
->persistent()
->actions([
Action::make('subscribe')
->button()
->url(route('subscribe'), shouldOpenInNewTab: true),
])
->send();

return; // Stop further execution
}

// If subscribed, redirect to create page
return redirect()->route('filament.user.resources.job-seeking-posts.create');
}
Want results from more Discord servers?
Add your server