public function store(Request $request) {
$data = $request->all();
// ...
}The Request object in Laravel holds all the data sent by the user in the HTTP request. This includes form inputs, query strings, cookies, and headers. The controller uses this data to decide what to do next.
Route::get('/greet', function (Request $request) { $name = $request->query('name', 'Guest'); return "Hello, $name!"; });
The query method on the Request object fetches the 'name' parameter from the URL. If it is missing, it returns the default value 'Guest'. Since no query parameter is sent, it outputs 'Hello, Guest!'.
public function handle(Request $request, Closure $next)
{
if ($request->user()->isAdmin())
return $next($request);
else
return redirect('/home');
}In PHP, each statement must end with a semicolon. The redirect('/home') line is missing a semicolon, causing a syntax error.
public function update(Request $request)
{
$validated = $request->validate([
'email' => 'required|email',
'age' => 'required|integer|min:18'
]);
// ...
}If the 'age' field is not sent in the request, Laravel treats it as missing and fails validation because 'integer|min:18' requires the field to be present and valid. Marking it as 'nullable|integer|min:18' allows it to be missing.
In Laravel's MVC, the HTTP request carries user data and context. Proper handling allows controllers and middleware to respond correctly, making request handling the foundation of application behavior.