How can I get id from url with Request $request? (Laravel 5.3)
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# How Can I Get ID from URL with Request $request? (Laravel 5.3)
As a senior developer working within the Laravel ecosystem, dealing with routing and request parameters is a daily occurrence. When you are building dynamic web applications, understanding how Laravel maps URLs to controller methods is crucial. The scenario presented—trying to extract an ID from a URL using `$request->input()` and failing—is a common point of confusion for developers transitioning between MVC concepts and Laravel's routing architecture.
This post will diagnose why your attempt to retrieve the ID resulted in `null` and show you the correct, idiomatic ways to access dynamic route parameters in your controller.
## The Misconception: Path Parameters vs. Query Parameters
The core issue lies in confusing two different types of data that are passed via a URL: **Path Parameters** and **Query Parameters**.
1. **Path Parameters (URL Segments):** These are variables embedded directly into the structure of the route itself, defining *which* resource you are requesting.
* Example Route: `/users/{user}`
* The `{user}` part is a segment of the URL path.
2. **Query Parameters:** These are optional filters or data passed *after* the `?` symbol in the URL, used for filtering or pagination.
* Example URL: `/users?sort=name&page=2`
In your specific case, when you define a route like `Route::get('message/inbox/detail/id/{id}', ...)` and access it via `http://myshop.dev/message/inbox/detail/id/58cadfba607a1d2ac4000254`, the value `58cadfba607a1d2ac4000254` is a **Path Parameter**, not a Query Parameter.
Because it is a path parameter, Laravel automatically injects this value directly into your controller method as an argument, bypassing the need to look inside the `$request` object for input. This is a fundamental feature of Laravel routing that promotes cleaner code, aligning perfectly with the principles taught by the [Laravel Company](https://laravelcompany.com).
## The Correct Solution: Injecting Parameters Directly
Instead of relying on `$request->input()`, you should leverage Laravel's ability to inject route variables directly into your controller method signature. This is the most direct, safest, and cleanest way to access dynamic IDs from the URL.
Let's correct your controller implementation based on your provided setup:
### Step 1: Update the Route Definition (If Necessary)
Ensure your route is correctly defined to capture the variable:
```php
// routes/web.php
Route::get('message/inbox/detail/id/{id}', ['as' => 'message.inbox.detail.id', 'uses' => 'MessageController@detail']);
```
### Step 2: Update the Controller Method (The Fix)
Modify your controller method to accept the dynamic segment as a typed variable:
```php
// app/Http/Controllers/MessageController.php
use Illuminate\Http\Request;
class MessageController extends Controller
{
public function detail(string $id) // <-- Capture the ID directly here!
{
// $id now holds the value from the URL segment (e.g., '58cadfba607a1d2ac4000254')
dd($id);
// You can now use this ID to fetch data from your database
$message = Message::findOrFail($id);
return view('message.detail', ['message' => $message]);
}
}
```
By defining the parameter in the method signature, Laravel handles the entire extraction process before your code even executes. This eliminates the need to manually parse the request object for path segments, making your code more readable