How to Get Query Strings Value in Laravel?

By Hardik Savani November 5, 2023 Category : Laravel

Hello Friends,

This example is focused on laravel get query string parameters. We will use laravel get query string. This article goes in detailed on laravel route query string. If you have a question about laravel query string sql then I will give a simple example with a solution. Alright, let us dive into the details.

A query string parameter is a part of a URL that contains additional information that is appended to the end of the URL after a question mark (?). It is used to pass data from one page to another or to a server, typically for the purpose of modifying the behavior or output of the page or server.

A query string parameter consists of a key-value pair separated by an equal sign (=), and multiple parameters are separated by an ampersand (&). For example, in the URL http://example.com/search?id=23&name=hardik, the query string parameters are id=23 and name=hardik. Here, id and name are the keys, and 23 and hardik are their respective values.

In Laravel, you can retrieve query string parameters using the request() helper method or through the Request object. Here are some examples:

we can get query string parameters in laravel 6, laravel 7, laravel 8, laravel 9 and laravel 10 application.

Let's see bellow examples:

Example 1: using Request Object

<?php

namespace App\Http\Controllers;

use Illuminate\Http\RedirectResponse;

use Illuminate\Http\Request;

class UserController extends Controller

{

/**

* Store a new user.

*/

public function store(Request $request): RedirectResponse

{

$name = $request->name;

$id = $request->id;

return redirect('/users');

}

}

Example 2: using request() helper

<?php

namespace App\Http\Controllers;

use Illuminate\Http\RedirectResponse;

use Illuminate\Http\Request;

class UserController extends Controller

{

/**

* Store a new user.

*/

public function store(Request $request): RedirectResponse

{

$name = request()->name;

$id = $request()->id;

return redirect('/users');

}

}

Example 3:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\RedirectResponse;

use Illuminate\Http\Request;

class UserController extends Controller

{

/**

* Store a new user.

*/

public function store(Request $request): RedirectResponse

{

$input = $request->all();

return redirect('/users');

}

}

I hope it can help you...

Tags :
Shares