Laravel 9 Redirect Route with Query String Parameters

Reading Time: 3 minutes
4,020 Views

Inside this article we will see the concept of route redirection from one location to other location with query string values.

query string is a part of a uniform resource locator (URL) that assigns values to specified parameters. A query string commonly includes fields added to a base URL by a Web browser or other client application.

This tutorial will be very easy to understand this small concept. We will discuss Laravel 9 redirect route with query string parameters.

Examples of Query Strings are:

/?name=Sanjay&email=sample@gmail.com 

?status=1&location=india

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.

Add Routes

Open web.php file from /routes folder. Add these routes into it.

//...

use App\Http\Controllers\SiteController;

Route::get("call-me", [SiteController::class, "fromRoute"])->name("call.from");
Route::get("call-to", [SiteController::class, "toRoute"])->name("call.to");

//...

Create Controller

Open project into terminal and run this artisan command.

$ php artisan make:controller SiteController

It will create SiteController.php file inside /app/Http/Controllers folder.

Open SiteController.php and add your route redirection logic.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SiteController extends Controller
{
    public function fromRoute()
    {
        return redirect()->route('call.to', ['name' => 'Ashish', 'age' => '28']);
    }

    public function toRoute(Request $request)
    {
        // Get all query string parameters
        print_r($request->all());
    }
}

This line of code helps to redirect()->route(‘call.to’, [‘name’ => ‘Ashish’, ‘age’ => ’28’]);

Redirect to – call.to named route.

Application Testing

Run this command into project terminal to start development server,

php artisan serve

URL – http://127.0.0.1:8000/call-me

Automatically it redirects to http://127.0.0.1:8000/call-to?name=Ashish&age=28

We hope this article helped you to learn about Laravel 9 Redirect Route with Query String Parameters 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