📖 Scenario: You are building a simple Laravel web application where users submit a form with their name and email. You want to make sure the data is valid before saving it.
🎯 Goal: Create a Laravel controller method that uses the validate method on the request to check that the name is required and at least 3 characters long, and the email is required and a valid email address.
📋 What You'll Learn
Create a controller method named store that accepts a Request object
Use the validate method on the $request to validate name and email
The name field must be required and have a minimum length of 3
The email field must be required and be a valid email
Return a simple success message after validation
💡 Why This Matters
🌍 Real World
Validating user input is essential in web applications to prevent bad data and security issues.
💼 Career
Laravel developers frequently use the validate method on requests to enforce data rules before processing.
Progress0 / 4 steps
1
Create the controller method with Request parameter
Create a public method called store inside a controller that accepts a Request parameter named $request.
Laravel
Hint
Remember to declare the method as public and accept Request $request as the parameter.
2
Add validation rules using the validate method
Inside the store method, use $request->validate() to validate that name is required and minimum 3 characters, and email is required and a valid email.
Laravel
Hint
Use an array inside validate() with keys 'name' and 'email' and their validation rules.
3
Add a success response after validation
After the $request->validate() call, return a string 'Validation passed!' from the store method.
Laravel
Hint
Simply return the string after validation to confirm success.
4
Complete the controller with namespace and use statements
Ensure the controller file starts with the correct namespace App\Http\Controllers; and includes use Illuminate\Http\Request; at the top.
Laravel
Hint
The namespace and use statements must be at the top of the file before the class.
Practice
(1/5)
1. What does the validate method on a Laravel request do?
easy
A. It saves the user input directly to the database.
B. It checks if the user input meets specified rules and returns errors if not.
C. It formats the user input into JSON automatically.
D. It sends an email notification when input is received.
Solution
Step 1: Understand the purpose of validate
The validate method is designed to check user input against rules.
A. The validate method should not return anything.
B. The 'email' rule is missing for the 'email' field.
C. The array brackets are missing around the rules.
D. The 'required' rule for 'password' is not quoted as a string.
Solution
Step 1: Check the syntax of rules for each field
Rules must be strings inside quotes, e.g., 'required|min:8'.
Step 2: Identify missing quotes in 'password' rules
The 'password' rules are not quoted, causing a syntax error.
Final Answer:
The 'required' rule for 'password' is not quoted as a string. -> Option D
Quick Check:
Rules must be strings in quotes [OK]
Hint: Always quote validation rules as strings [OK]
Common Mistakes:
Forgetting quotes around rules
Misplacing array brackets
Thinking validate returns void always
5. You want to validate a request so that the 'username' is required, must be a string, and unique in the 'users' table, but only if the user is creating a new record (not updating). Which validation rule is correct?
hard
A. $request->validate(['username' => 'required|string|unique:users']);
B. $request->validate(['username' => 'required|string|unique']);
C. $request->validate(['username' => 'required|string|unique:users,username,' . $user->id]);
D. $request->validate(['username' => 'required|string|unique:users.username']);
Solution
Step 1: Understand the unique rule for new records
For new records, the unique rule is simply 'unique:table' or 'unique:table,column'.
Step 2: Identify the rule that applies only for new records
$request->validate(['username' => 'required|string|unique:users']); uses 'unique:users' which checks uniqueness in the users table without exceptions.
Step 3: Eliminate incorrect options
A omits the table name, C is for updates (ignores current user ID), D uses invalid syntax 'unique:users.username' (dot instead of comma).
Final Answer:
$request->validate(['username' => 'required|string|unique:users']); -> Option A
Quick Check:
Unique rule with table name only = new record check [OK]
Hint: Use 'unique:table' for new record uniqueness [OK]
Common Mistakes:
Using update syntax for new records
Omitting table name in unique rule
Confusing unique rule format
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.