#how can disable the createAction If?

2 messages · Page 1 of 1 (latest)

eternal jacinth
#

I need to block the Create User button if the logged in user does not have an associated business, I want the button to be visible but when clicked it does not redirect and shows a Filament Notification, I tried this code but it does not work for me, it does not go through the validation, I tried with dd() but it still doesn't happen, is there something I'm forgetting or missing? This is my code!

<?php
namespace App\Filament\Resources\UserResource\Pages;

use App\Filament\Resources\UserResource;
use Filament\Actions;
use Filament\Actions\Action;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\ListRecords;

class ListUsers extends ListRecords
{
    protected static string $resource = UserResource::class;

    protected function getHeaderActions(): array
    {
        return [
            Actions\CreateAction::make()
            ->before(function (Actions\CreateAction $action) {
                // Validar si el usuario tiene negocios asociados
                if ($this->shouldDisableCreate()) {
                        dd('hola');
                        Notification::make()
                            ->warning()
                            ->title('Acción no permitida')
                            ->body('No puedes crear usuarios sin primero tener un negocio asociado.')
                            ->persistent()
                            ->actions([
                                Action::make('create')
                                    ->button()
                                    ->label('Crear usuario'),
                            ])
                            ->send();
                        // Detener la acción
                        $action->halt();
                    }
                }),
        ];
    }

    private function shouldDisableCreate(): bool
    {
        return !auth()->user()->businesses()->exists();
    }
}
crude rapids