Performance: Validate method on request
MEDIUM IMPACT
This affects server response time and user experience by validating input before processing.
<?php
public function store(Request $request) {
$validated = $request->validate([
'email' => 'required|email',
'name' => 'required|string|max:255'
]);
User::create($validated);
}<?php
public function store(Request $request) {
$data = $request->all();
if (empty($data['email']) || !filter_var($data['email'], FILTER_VALIDATE_EMAIL)) {
return back()->withErrors(['email' => 'Invalid email']);
}
// manual validation for each field
// ...
User::create($data);
}| Pattern | CPU Usage | Response Delay | Code Complexity | Verdict |
|---|---|---|---|---|
| Manual validation with if checks | High | Longer | Verbose and error-prone | [X] Bad |
| Laravel validate() method | Low | Shorter | Concise and optimized | [OK] Good |