Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

pass multiple parameters in url laravel

How to pass multiple parameters in url laravel ?

In this tutorial, we will learn How to pass multiple parameters in URL in Laravel.

Sometimes we need to pass multiple parameters in URL and then we get parameters from the controller and after that perform an action. Basically, It uses for Custom URL or Pretty URL Structure in Laravel application.

We can pass multiple parameters through the named route to the controller method.

Let’s see an Example, will first define a route with multiple parameters.

Define Routes

In the web.php file we are passing 2 parameters, you can pass multiple parameters in routes, we have a products table in our database, in the products table, we have multiple fields like id, product_no, product name, product description.

Table Structure

pass multiple parameters in url laravel

We will pass id and product_no in the URL and then we access these parameters from the controller.

routes/web.php :


#Passing Multiple Parameters
Route::get('/view-product/{id}/{product_no}','ProductController@ViewProduct')->name('view.product'); 

Setup a link (URL)

Now, we can setup a named route link in view as below:

resources/views/product/list.blade.php

<table class="table">
            <thead>
               <tr>
                  <th>S.no</th>
                  <th>Order / Product No.
                  <th>Product Name</th>
                  <th>Product Description</th>
                  <th>Action</th>
               </tr>
            </thead>
            <tbody>
               @foreach($productList as $key => $data)
               <tr>
                  <td>{{ $key+1 }}</td>
                  <td>{{$data->product_no}}</td>
                  <td>{{$data->name}}</td>
                  <td>{{$data->description}}</td>
                  <td><a href="{{route('view.product',[$data['id'],$data['product_no']])}}">View Product</a></td>
               </tr>
            </tbody>
            @endforeach
         </table>
pass multiple parameters in url laravel
<a href="{{route('view.product',[$data['id'],$data['product_no']])}}">View Product</a>

Get The Parameter

App\Http\Controllers\ProductController

Now, we access the parameter’s values in the controller.

    #Get Parameters
    public function ViewProduct($id,$product_no)
      {
        dd($id,$product_no);
      }

Output

pass multiple parameters in url laravel

In this tutorial, we learned “How to pass multiple parameters in url laravel”, I hope this article will help you with your Laravel application Project.

Read Also : How to generate unique Order numbers in Laravel ?