Laravel Tip: What's the Difference Between setParameter() and request->add()??

Confused between $request->route()->setParameter() and $request->request->add() in Laravel? This quick guide breaks down the difference, use cases, and best practices. Learn when to use each to keep your code clean, efficient, and Laravel-ready.

Laravel Tip: What's the Difference Between setParameter() and request->add()?? Image

When working with Laravel requests, you might come across these two similar-looking lines:

$request->route()->setParameter('layout', $layout);
$request->request->add(['layout' => $layout]);

1. setParameter(): Adds a Route Parameter

Example:

$request->route()->setParameter('layout', 'grid');

Now you can access it using:

$request->route('layout'); // returns 'grid'
  • What it does
    This modifies the route parameter, not the usual input data. Under the hood, Laravel’s Route object keeps a list of parameters that were extracted from the URI (and also any you manually set). By calling setParameter(), you’re telling Laravel “for the currently matched route, pretend that there was a {layout} wildcard in the URI whose value is $layout.”

  • Where it shows up

    • In your controller or middleware, you can later retrieve it via $request->route('layout')

    • If your route definition includes something like /posts/{layout}, URL generation (route('posts.show', ['layout' => 'grid'])) and URL matching will use this parameter.

  • Typical use cases

    • Dynamically influencing route model binding

    • Generating URLs with a “virtual” route parameter

    • Middlewares that alter how the route is resolved


2. request->add(): Adds POST/Form Input

Example:

$request->request->add(['layout' => 'grid']);

Now you can access it using:

$request->input('layout'); // returns 'grid'
  • What it does
    This injects data into the “request payload” bag—that is, the same place where $_POST (and $_PUT, etc.) lives. In Symfony (which Laravel’s HTTP layer is built on), the ParameterBag $request holds form‐input data. So calling add() simply makes layout appear as if the client had submitted it in the body of the request.

  • Where it shows up

    • In your controller or middleware, you can retrieve it via $request->input('layout') or $request->get('layout')

    • Validation ($this->validate($request, ['layout' => 'required|string'])) will see it as part of the submitted data

    • Mass‐assignment via $model->fill($request->all()) will include it

  • Typical use cases

    • Adding or overriding form data before validation

    • Injecting default values into the request payload

    • Preparing input for service classes, import routines, etc.


Summary

Aspectroute()->setParameter()request->add()
BagRoute parametersHTTP request payload ($_POST etc.)
Retrieval$request->route('layout')$request->input('layout')
Affects URL gen?Yes (in URL generation and route matching)No
Validation sees it?No (unless you manually map route params in)Yes
Use when…You want to influence routing/URL buildingYou need to add or override input data

Choose the one that matches your goal:

  • If you need the value to act like a URL/route parameter (in route model‐binding, URL generation, middleware logic that reads route params), use setParameter.

  • If you need the value to act like submitted form data (for validation, mass‐assignment, or business logic off $request->input()), use add().

Pro Tip

Always choose based on context:

  • Need it for routing or URLs? Use setParameter().

  • Need it for form validation or business logic? Use request->add().


💡 Stay sharp and explore more Laravel tips at Tutorial Tools!

Tags

Do you Like?