Laravel 8 How to Redirect Route with Query String Params

Share this Article
Reading Time: 2 minutes
3,112 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.


Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project.

If your system doesn’t has composer Installed, Learn Composer Installation Steps.

Here is the complete command to create a laravel project-

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

After following these steps we can install a Laravel application into system.

To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel already installed at 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’]); R

Redirect to – call.to named route.


Application Testing

Open project to terminal and type the command 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

Find More on Laravel 8 Articles here