Laravel 8 How to Redirect Route with Query String Params

Reading Time: 3 minutes
13,899 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 8 How to redirect route with query string parameters.

Learn More –

URL
http://127.0.0.1:8000/call-to?name=Ashish&age=28

Query String values
?name=Ashish&age=28

Let’s get started.


Laravel Installation

We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.

Here is the command to create a laravel project-

composer create-project --prefer-dist laravel/laravel blog

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.

//...

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 8 How to Redirect Route with Query String Params 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