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;
    }
}
Was this page helpful?