Ad

How To Pass Two Different ID's In A Route Laravel?

- 1 answer

I have a simple app, I need to pass two different ID's id and code_id in a route, here is my solution I have tried so far

view

  <a target="_blank" rel="nofollow noreferrer" href="{{ route('settings.code', $settings->id,  $settings->code_id) }}">{{ __('Code') }}</a>

Here is route config

    Route::get('settings/code/{id}/{code_id}', ['as' => 'settings.code', 'uses' => '[email protected]']);

Here is my function in a controller

 public function code($code_id, $id)
     {   
         $settings = Setting::find($code_id, $id);


          dd($settings);


         return view('pages.settings.code', compact('settings'));

     }

Here is the error I get

Missing required parameters for [Route: settings.code] [URI: settings/code/{id}/{code_id}]. (0)

What is wrong with my code?

Ad

Answer

First you should pass an array as 2nd argument to route() method:

{{ route('settings.code', ['id' => $settings->id,  'code_id' => $settings->code_id]) }}

And note that:

Route parameters are injected into route callbacks / controllers based on their order - the names of the callback / controller arguments do not matter.

So you should swap the arguments of your controller's method:

public function code($id, $code_id)
{
    //...   
}
Ad
source: stackoverflow.com
Ad