Laravel5 redirect to a route with query string parameters example

By | July 6, 2018

Hi guys,

Sometimes when we are working on an application then we generally need to redirect to a route with some parameters as a query string. As we all know this is a small thing to do but sometimes we stuck at this point and it takes a lot of valuable time. So in this article i am going to tell you laravel5 redirect to a route with query string parameters.

Here i am giving an example to redirect in laravel application. Let’s assume we need to redirect to a url given below.

http://www.phpzone.in/pdata?pid=2&pname=test&year=2018

Now we know where we have to redirect with what parameter. Here there are three parameters pid, pname and year. so let’s go to the controller logic.

Example Controller

 

namespace App\Http\Controllers;


use Illuminate\Http\Request;


class ProductController extends Controller
{
  

    public function getProduct()
    {

        return redirect()->route('pdata',['pid'=>2,'pname'=>'phpzone.in','year'=>'2018'])
                ->with('pmessage','You have redirected');
    }


}

In the above example, i have created a ProductController class with getProduct() method whenever this method would be called, It would be redirected to pdata route with the parameters in query string and we will get the response as we expected.