Laravel provides several different approaches to validate your application’s incoming data. Ensuring data is valid before storing it is crucial for application integrity.
#Basic Validation with Controllers
The most common way is using the validate method available on Illuminate\Http\Request objects within your controller methods.
use Illuminate\Http\Request;
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'required|unique:posts|max:255',
'body' => 'required',
'publish_at' => 'nullable|date',
]);
// The blog post is valid... proceed with storing
}
If validation fails, Laravel automatically redirects the user back to the previous location (usually a form) with the validation errors flashed to the session. For AJAX requests, it returns a JSON response with a 422 status code.
#Form Requests for Complex Validation
For more complex validation scenarios, you can create dedicated Form Request classes. These classes contain their own validation and authorization logic.
php artisan make:request StorePostRequest
Then, type-hint the request in your controller method:
use App\Http\Requests\StorePostRequest;
public function store(StorePostRequest $request)
{
// The incoming request is valid...
// Retrieve the validated input data...
$validated = $request->validated();
}
Laravel offers a wide range of built-in validation rules, and you can also create your own custom rules.