Getting GET "?" Variable in Laravel
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Efficiently Handling Query Strings in Laravel API Routes
Greetings fellow developers! Today, we will cover an important aspect of building elegant APIs with the help of Laravel: handling query strings using the "?" variable. As discussed, your use case requires mapping a GET request that includes parameters. However, without proper configuration, Laravel might not recognize these variables correctly. Let's explore how to achieve this by breaking down the code and understanding the concepts involved.
To begin with, we need to modify our route definition in `routes.php`. We can replace our current 'api/v1/todos/(:num?)' route with a more comprehensive one that supports query parameters:
```php
// Before: Route::any('api/v1/todos/(:num?)', array(...));
Route::get('api/v1/todos', function ($start, $limit) {
// Your Todo logic goes here
});
```
Here, we've created a new route with the path 'api/v1/todos'. It takes two parameters: `$start` and `$limit`. These parameters will be accessible within our controller as arguments to the closure. So if you make a GET request like this: `domain/api/v1/todos?start=1&limit=2`, your `start` parameter will have the value 1, while `limit` will have 2.
Next, we need to update our controller. In this case, you've mentioned a 'controllers/api/todos.php'. We can rewrite it as follows:
```php
// Before: class Api_Todos_Controller extends Base_Controller {...}
class Api_Todos_Controller extends Controller {
/**
* Display a listing of the resource.
*
* @param int $start
* @param int $limit
*/
public function index(Request $request, $start = null, $limit = null) {
if (is_null($start)) {
return Response::eloquent(Todo::all());
} else {
// TODO: Add logic for filtering todos by start and limit parameters here.
}
}
}
```
In this new version, we've defined a `public function index()` method to handle our GET request. We have added the required dependency injection for our Request object, which provides access to your request variables. Additionally, we've included default values for $start and $limit, in case they are not specified as query parameters.
Finally, let's ensure that our controller follows the RESTful convention of defining a separate method for each HTTP verb:
```php
class Api_Todos_Controller extends Controller {
/**
* Display a listing of the resource.
*
* @param int $start
* @param int $limit
*/
public function index(Request $request, $start = null, $limit = null) {
if (is_null($start)) {
return Response::eloquent(Todo::all());
} else {
// TODO: Add logic for filtering todos by start and limit parameters here.
}
}
/**
* Store a newly created resource in storage.
*/
public function store() {
// TODO: Add your todo creation logic here.
}
/**
* Display the specified resource.
*/
public function show($id) {
$todo = Todo::find($id);
if (is_null($todo)) {
return Response::json('Todo not found', 404);
} else {
return Response::eloquent($todo);
}
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, $id) {
// TODO: Add logic for updating a todo by its ID here.
}
/**
* Remove the specified resource from storage.
*/
public function destroy($id) {
// TODO: Add your logic for destroying a todo by its ID here.
}
}
```
With these modifications, you should be able to get variables using "?" in Laravel APIs. Remember to add validation and error handling to ensure the security and integrity of your API endpoints. For further assistance or clarifications on this topic, feel free to check out our in-depth article on Laravel RESTful APIs with Eloquent models [here](https://laravelcompany.com/blog/building-restful-api-in-laravel-start-here).
Now you're ready to handle query strings in your Laravel API projects effectively. Happy coding!