0
0
Laravelframework~30 mins

Request validation basics in Laravel - Mini Project: Build & Apply

Choose your learning style9 modes available
Request validation basics
📖 Scenario: You are building a simple Laravel web application where users can submit their contact information through a form. To keep the data clean and safe, you need to validate the user input before saving it.
🎯 Goal: Build a Laravel controller method that validates a request with specific rules for name, email, and age fields.
📋 What You'll Learn
Create a controller method named store that accepts a Request object
Add validation rules for name, email, and age fields
Use Laravel's $request->validate() method for validation
Return the validated data as an array
💡 Why This Matters
🌍 Real World
Validating user input is essential in web applications to ensure data integrity and security before processing or saving it.
💼 Career
Understanding Laravel request validation is a key skill for backend developers working with Laravel to build robust and secure web applications.
Progress0 / 4 steps
1
Create the store method with a Request parameter
Create a public method called store inside a controller class that accepts a parameter named $request of type Request.
Laravel
Need a hint?

Define a public function named store that takes Request $request as input.

2
Define validation rules array
Inside the store method, create a variable named $rules that is an array with these exact keys and values: 'name' => 'required|string|max:255', 'email' => 'required|email', and 'age' => 'nullable|integer|min:18'.
Laravel
Need a hint?

Use an associative array with keys name, email, and age and their validation rules as strings.

3
Validate the request using $request->validate()
Use the $request->validate() method with the $rules array to validate the incoming data inside the store method. Store the result in a variable named $validatedData.
Laravel
Need a hint?

Call $request->validate($rules) and assign the result to $validatedData.

4
Return the validated data
At the end of the store method, return the $validatedData variable.
Laravel
Need a hint?

Use return $validatedData; to send back the validated input.