We use request data to get information users send to our app, like form inputs or URL details.
0
0
Accessing request data in Laravel
Introduction
When a user submits a form and you want to get their input.
When you need to read query parameters from a URL.
When handling JSON data sent from a client app.
When you want to check headers or cookies sent with a request.
Syntax
Laravel
<?php
use Illuminate\Http\Request;
public function example(Request $request) {
$value = $request->input('key');
// or
$value = $request->get('key');
// or
$value = $request->query('key');
// or
$value = $request->post('key');
}$request->input('key') gets data from any input source (query, post, json).
$request->query('key') gets only query string parameters.
Examples
Gets the value of 'name' from any request input.
Laravel
<?php $name = $request->input('name');
Gets 'page' from URL query parameters, defaults to 1 if missing.
Laravel
<?php $page = $request->query('page', 1);
Gets 'email' from POST data only.
Laravel
<?php
$email = $request->post('email');Gets all input data as an array.
Laravel
<?php $allData = $request->all();
Sample Program
This controller method gets 'username' and 'email' from the request and returns them as a string.
Laravel
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; class UserController extends Controller { public function store(Request $request) { $username = $request->input('username'); $email = $request->input('email'); return "Username: $username, Email: $email"; } }
OutputSuccess
Important Notes
Always validate request data before using it to keep your app safe.
You can provide a default value as a second argument to input() if the key might be missing.
Use $request->all() carefully; it returns all input data which might be large or sensitive.
Summary
Use $request->input('key') to get user data from forms or URLs.
Use specific methods like query() or post() to get data from certain parts of the request.
Always check and clean request data before using it in your app.