Passing page URL parameter to controller in Laravel 5.2
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# Passing Page URL Parameters to the Controller in Laravel 5.2
When building dynamic web applications with Laravel, handling data passed through the URL—specifically GET parameters—is a fundamental skill. As developers, we often need to filter database queries based on user input, and understanding how to bridge the gap between the URL and the controller logic is crucial.
In this post, we will walk through the process of taking parameters like `?order` and `?type` from a request URL, passing them into our controller action, using them to query the database, and finally displaying the results on our index page.
## The Laravel Approach: Utilizing the Request Object
The key to accessing any data sent via an HTTP request in Laravel is the `Illuminate\Http\Request` object. This object is automatically injected into your controller methods, making parameter retrieval straightforward and secure. It acts as the gateway for all incoming request data.
For a route defined like `/index?order=asc&type=products`, we need to extract the values of `order` and `type` within our controller method.
## Step-by-Step Implementation
Let's assume you have a resource, perhaps an `Item` model, that you are querying based on these parameters.
### 1. Define the Route (web.php)
First, ensure your route is set up to accept these parameters. Since we are using GET requests with query strings, the structure remains simple:
```php
// routes/web.php
use App\Http\Controllers\ItemController;
Route::get('/index', [ItemController::class, 'index'])->name('items.index');
```
### 2. Retrieve Parameters in the Controller
In your controller method, you inject the `Request` object (Laravel handles this dependency injection automatically) and use its `input()` method to safely retrieve the query parameters.
```php
// app/Http/Controllers/ItemController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Item; // Assuming you are using Eloquent models
class ItemController extends Controller
{
public function index(Request $request)
{
// 1. Retrieve the parameters from the request object
$order = $request->input('order', 'default'); // Provide a default value for safety
$type = $request->input('type', 'all');
// 2. Use these parameters to query the database (Eloquent example)
$items = Item::where('type', $type)
->orderBy($order)
->get();
// 3. Pass the results to the view
return view('index', compact('items', 'order', 'type'));
}
}
```
### 3. Display Data in the View (index.blade.php)
Finally, you pass the retrieved data to your Blade template using the `compact()` helper. This makes the variables available directly within the view context.
```html
{{-- resources/views/index.blade.php --}}
Items Ordered by {{ $order }} and Type: {{ $type }}
@if ($items->isEmpty())No items found matching your criteria.
@else-
@foreach ($items as $item)
- ID: {{ $item->id }}, Name: {{ $item->name }} @endforeach