Home > Software engineering >  How to pass multiple parameters to route
How to pass multiple parameters to route

Time:02-04

Hello i have this function called show and it has 2 parameters id and options this is the code from blade.php :

<a href="{{route('orders.show',$order->id,'1')}}">
<button>Edit</button>
</a>

The number one is static but i have another button that has value 2 now this is the code from the controller:

public function show($id, $option)
{
    $orders = Order::with('client', 'products')->where('id', $id)->firstOrFail();
    $clientList = Client::all();
    $productList = Product::all();
    switch ($option)
    {
        case 1:
        {
            return view('orders.edit', compact('orders', 'clientList', 'productList'));
            break;
        }
        case 2:
        {
            return view('orders.show', compact('orders', 'clientList', 'productList'));
            break;
        }
    }
}

But when i execute teh code i get this error:

Too few arguments to function App\Http\Controllers\OrderController::show(), 1 passed in /home/user/Desktop/k_project/vendor/laravel/framework/src/Illuminate/Routing/Controller.php on line 54 and exactly 2 expected

Basically the second argument is not passing i used another method like this:

<a href="{{ route('student.detail', ['id' => 5, 'parameter' => 'advanced-web-hindi', 'name' => 'anmol']) }}">Student detail</a>

From this page: Page

While before i passing just one value $id and it worked fine but now i need a second parameter to pass.

This is my route definition that just one value passing $id works but with two values passing $id and $option will make error:

Route::resource('orders', OrderController::class);

CodePudding user response:

Your route has 3 parameters id, parameter, name. You can have two solutions for that.

1. First Solution

route.php

Route::get('/orders/show/{id}', 'OrderController@show')->name('orders.show');

OrderController.php

public function show(Request $request, $id)
{
    $name = $request->input('name');
    $parameter = $request->input('parameter');
    ....
}

2. Second Solution

route.php

Route::get('/orders/show/{id}/{name}/{parameter}', 'OrderController@show')->name('orders.show');

OrderController.php

public function show($id, $name, $parameter)
{
    ....
}
  • Related