Add the bail rule to validation rules to stop executing subsequent expensive checks (like database queries) as soon as the first validation rule fails.
By default, Laravel validates an attribute against all specified rules. If an invalid email format is submitted, Laravel still runs expensive unique:users,email database queries on that invalid string, wasting database CPU cycles.
The bail rule stops validation on an attribute upon its first failure.
Using bail in Form Requests
public function rules(): array
{
return [
'email' => [
'bail', // Stops on first error!
'required',
'email',
'unique:users,email', // Expensive SQL query ONLY runs if 'email' is valid!
],
'account_id' => [
'bail',
'required',
'numeric',
'exists:accounts,id', // Database check runs only if numeric
],
];
}
Summary
- Prevents running expensive database
uniqueandexistsqueries on malformed input. - Returns only the first relevant validation error message to the user.
- Placed as the first rule in the attribute's rule array.
Related Tips
View all tips →Optimize Large Wildcard Array Validation in Laravel 13.24
Laravel 13.24 introduces dramatic performance optimizations for validating large nested arrays with wildcard rules, reducing validation execution time from 85 seconds to under 1 second.
Verify Existing User Credentials with the current_password Validation Rule
Use the current_password validation rule to require users to confirm their existing password before performing sensitive account actions.