F
Filamentβ€’7mo ago
Mehmet K.

How can I prevent the record creation if the conditions are not met in the BeforeCreate method?

I have a credit control system, and I want to prevent the user from creating a record if their credit is insufficient. Additionally, I would like to throw a notification with an error message. However, the code below is not working. Can you help me?
protected function BeforeCreate()
{
$user = Auth::user();

// Retrieve the user's credit status from the database
$credits = $user->credits;

if ($credits >= 2) {
return true; // Continue with the record creation
} else {
// Insufficient credit, prevent record creation and throw an error notification
Notification::make()
->title('Insufficient Credit')
->error()
->send();

// Return false to prevent record creation
return false;
}
}
protected function BeforeCreate()
{
$user = Auth::user();

// Retrieve the user's credit status from the database
$credits = $user->credits;

if ($credits >= 2) {
return true; // Continue with the record creation
} else {
// Insufficient credit, prevent record creation and throw an error notification
Notification::make()
->title('Insufficient Credit')
->error()
->send();

// Return false to prevent record creation
return false;
}
}
5 Replies
toeknee
toekneeβ€’7mo ago
Is this in V3?
Mehmet K.
Mehmet K.β€’7mo ago
Yes V3
Ehsan
Ehsanβ€’7mo ago
For filament v3, you can use
protected function handleRecordCreation(array $data): Model
{
$user = Auth::user();

// Retrieve the user's credit status from the database
$credits = $user->credits;

if ($credits >= 2) {
return static::getModel()::create($data);
} else {
// Insufficient credit, prevent record creation and throw an error notification
Notification::make()
->title('Insufficient Credit')
->error()
->send();

// Return false to prevent record creation
return false;
}
}
protected function handleRecordCreation(array $data): Model
{
$user = Auth::user();

// Retrieve the user's credit status from the database
$credits = $user->credits;

if ($credits >= 2) {
return static::getModel()::create($data);
} else {
// Insufficient credit, prevent record creation and throw an error notification
Notification::make()
->title('Insufficient Credit')
->error()
->send();

// Return false to prevent record creation
return false;
}
}
Alternatively, you can also use halt the creation process like this
protected function beforeCreate(): void
{
$user = Auth::user();

// Retrieve the user's credit status from the database
$credits = $user->credits;

if ($credits < 2) {
// Insufficient credit, prevent record creation and throw an error notification
Notification::make()
->title('Insufficient Credit')
->error()
->send();

$this->halt();
}
}
protected function beforeCreate(): void
{
$user = Auth::user();

// Retrieve the user's credit status from the database
$credits = $user->credits;

if ($credits < 2) {
// Insufficient credit, prevent record creation and throw an error notification
Notification::make()
->title('Insufficient Credit')
->error()
->send();

$this->halt();
}
}
Mehmet K.
Mehmet K.β€’7mo ago
Thank you Ehsan πŸ™‚
Ehsan
Ehsanβ€’7mo ago
Happy to help