Performance: Controller methods and actions
This affects server response time and how quickly the browser receives data to render the page.
public function show($id) {
$user = User::find($id);
return view('user.profile', ['user' => $user]);
}public function show($id) {
$user = User::all()->where('id', $id)->first();
return view('user.profile', ['user' => $user]);
}| Pattern | Server Processing | Database Queries | Response Time | Verdict |
|---|---|---|---|---|
| Fetching all records then filtering | High CPU and memory | Inefficient, loads all data | Slow response | [X] Bad |
| Direct query for needed record | Low CPU and memory | Efficient single query | Fast response | [OK] Good |
show in Laravel?public function methodName() {}.public function greet($name) {
return "Hello, " . $name . "!";
}$controller->greet('Anna')?$name and concatenates it with strings.$name with 'Anna', so output is "Hello, Anna!".public function index()
{
return view('welcome')
}return view('welcome').public function userInfo()
{
$user = ['name' => 'John', 'email' => 'john@example.com'];
// Fill in the return statement
}response()->json() helper to return JSON responses.response()->json($user);. Others are invalid or incorrect syntax.Pass the test and this pattern moves unknown