In this tutorial you will learn about the Laravel Passing Multiple Parameters In Route to Controller and its application with practical example.
Today in this tutorial, we will learn to pass multiple parameters through named route to controller method. In this Laravel Passing Multiple Parameters In Route to Controller tutorial you will learn to pass multiple parameters using route to controller. You will also learn to define route with multiple parameters in laravel so that can access them in controller method.
Passing Multiple Parameters Using Route to Controller
Sometimes we need to pass multiple parameters in URL so that we can get those parameters in controller method to perform required action. In this example, will first define a route with multiple parameters and then we will add a controller method accepting multiple parameters. Then we will setup a link with named route having multiple parameters.
Define route with multiple parameters
Now we will learn to define route with multiple parameters.
Syntax (route):-
Below is basic syntax to define a get request route accepting multiple parameters:
1 |
Route::get('test/{paramter1}/{paramter2}', 'Somecontroller@somemethod'); |
Example:-
Now, lets define a example get route accepting multiple parameters as below:
1 |
Route::get('/posts/show-post/{id}/{name}','PostController@show_post')->name('post.show'); |
Define Controller Method With Multiple Parameters
Now we will learn to define a controller method accepting multiple parameters.
Syntax (Controller Method):-
Below is basic syntax for controller method accepting multiple parameters as below:
1 2 3 |
public function somemethod($paramter1, $paramter2){ //my code } |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php namespace App\Http\Controllers\Admin; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use DB; class PostController extends Controller { public function show_post($id,$name){ echo $id.' - '.$name; } } |
Define Named Route With Multiple Parameters
Now, we can setup a named route link in view as below:
1 |
<a class="btn btn-success" href="{{route('post.show',['id'=>$post->id,'name'=>$post->title])}}">Show</a> |