How to Get Request Parameters in Laravel 10 Tutorial

Reading Time: 4 minutes
292 Views

Retrieving request parameters in Laravel 10 is a crucial component of web development since it allows you to access data sent via client-side requests such as forms, URLs, and API calls. With real examples, we will walk you through the process of collecting request parameters in Laravel 10.

Laravel has a variety of methods and approaches for accessing request parameters, making it simple to retrieve and operate with user-submitted data.

Read More: How to Get Current Route Name in Laravel 10 Tutorial

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

What are Request Parameters in Laravel?

Request parameters, also known as route parameters or URL parameters in Laravel, are a means to capture and extract information from the URL of an incoming HTTP request. These parameters are used to pass data to the routes of your application, allowing you to design dynamic and configurable routes that respond to various input.

Request parameters are commonly available in the URL path or query string and can be accessed using your controller or route callback functions. Laravel includes methods for working with these parameters that are simple to use.

Here, you will see how to access request parameters of a submitted form inside a method.

How To get Request Parameters in Controller?

Two cases to get values,

  • All parameters
  • Specific parameters

Example #1

To get all request parameters.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller
{
    public function store(Request $request)
    {
        dd($request->all());
    }
}

Concept

$request->all()

Read More: How To Upload File with Progress Bar in Laravel 10 Tutorial

Output

array:4 [▼
  "_token" => "6P1sjqZ3REvEMeH7c58BAcmuc2WEmM8GXPessl2Z"
  "title" => "title"
  "body" => "body"
  "description" => "description"
]

Example #2

To get some specific request parameters.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller
{
    public function store(Request $request)
    {
        dd($request->only('title', 'body'));
    }
}

Concept

$request->only('title', 'body')

Output

array:2 [▼
  "title" => "title"
  "body" => "body"
]

In the same way you can access these values in middlewares as well.

How To get Request Parameters in Middleware?

To get all values,

public function handle(Request $request, Closure $next): Response
{
    dd($request->all());
    //...
}

To get single value,

public function handle(Request $request, Closure $next): Response
{
    dd($request->get("title"));
    //...
}

How To get Request Parameters in Blade Templates?

To get all values,

<p>{{ dd(request()->all()) }}</p>

Read More: How To Handle Exception in Laravel 10 Example Tutorial

To get single value,

<p>{{request()->get('param1')}}</p>

That’s it.

We hope this article helped you to learn about How to Get Request Parameters in Laravel 10 Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more