Laravel 10 Redirect Route with Query String Parameters

Reading Time: 3 minutes
333 Views

In Laravel 10, redirecting routes with query string arguments allows you to transmit data between pages while keeping a clean and user-friendly URL structure.

Query string arguments let you to send variables and values between routes, allowing for dynamic content and personalised user experiences.

This tutorial will walk you through the steps of redirecting routes with query string arguments in Laravel 10. We’ll look at how to create URLs with query string arguments, how to traverse between routes with the redirect() method, and how to properly transmit data between different portions of your application.

Read More: Laravel 10 Pagination with Bootstrap Example Tutorial

Examples of Query Strings are:

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

?status=1&location=india

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.

Setup Application 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' => 'Dhananjay', 'age' => '24']);
    }

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

Concept

redirect()->route('call.to', ['name' => 'Ashish', 'age' => '28']);

This line of code helps to redirect to “call.to” named route.

Read More: Laravel 10 HTTP cURL POST Request with Headers Tutorial

Additionally, you can redirect with custom message as well,

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SiteController extends Controller
{
    public function fromRoute()
    {
        return redirect()->route('call.to',['id' => 20, 'productid' => 30])
                ->with('info','Successfully, we are back with product ID.');
    }
}

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");

//...

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=Dhananjay&age=28

We hope this article helped you to learn about Laravel 10 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