Laravel 9 How to Get Request Parameter Tutorial

Reading Time: 4 minutes
1,899 Views

Inside this article we will see the concept i.e Laravel 9 How To get request parameter. Article contains classified information about getting request parameters in laravel. We will consider all cases like getting values into controller file, blade template files, get inside middlewares.

Tutorial is very simple and it works around Request object to work with get request parameters. Also this article is not any version specific. You can use this concept in any laravel versions.

Learn More –

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.

Get Request Parameter in Controller File

Here,

We will see two cases inside controllers to get request parameters.

Example #1

To get all request parameters.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class PostController extends Controller
{
    /**
     * Write Your Code..
     *
     * @return string
    */
    public function store(Request $request)
    {
        dd($request->all());
    }
}

Concept

$request->all()

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;
use Illuminate\Support\Facades\Route;

class PostController extends Controller
{
    /**
     * Write Your Code..
     *
     * @return string
    */
    public function store(Request $request)
    {
        dd($request->only('title', 'body'));
    }
}

Concept

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

Output

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

Get Request Parameter in Middleware File

Here,

We will see two cases inside middlewares to get request parameters.

Example #1

To get all request parameters.

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class CustomMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        dd($request->all());
        return $next($request);
    }
}

Example #2

To get some specific request parameters , the same process what is for controller we need to do.

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class CustomMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        dd($request->get('title'));
        return $next($request);
    }
}

Get Request Parameter in Blade File

Here,

We will see two cases inside blade templates to get request parameters.

Example #1

To get all request parameters.

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

Example #2

To get some specific request parameters , the same process what is for controller and middlewares we need to do.

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

We hope this article helped you to learn about Laravel 9 How to Get Request Parameter 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.