In Laravel, you can pass parameters to a controller in several ways, depending on the context and the HTTP request. Here are a few common methods:
Route Parameters:
php// routes/web.php
Route::get('/user/{id}', 'UserController@show');
php// app/Http/Controllers/UserController.php
public function show($id) {
// Controller logic here
}
{id}
in the URL will be automatically passed to the show
method.Request Object:
Illuminate\Http\Request
object to access parameters from the request. For example:php// app/Http/Controllers/UserController.php
use Illuminate\Http\Request;
public function show(Request $request) {
$id = $request->input('id');
// Controller logic here
}
Form Requests:
php// app/Http/Requests/StoreUserRequest.php
public function rules() {
return [
'name' => 'required',
'email' => 'required|email',
];
}
php// app/Http/Controllers/UserController.php
use App\Http\Requests\StoreUserRequest;
public function store(StoreUserRequest $request) {
// Validated data is available here
$name = $request->input('name');
$email = $request->input('email');
// Controller logic here
}
Query Parameters:
request()
helper function or the Illuminate\Http\Request
object:php// app/Http/Controllers/UserController.php
public function index() {
$name = request('name');
// Controller logic here
}
As for additional research, Laravel's official documentation is a great resource for understanding how to work with controllers and request data.
=== Happy Coding :)