How to pass two different ID's in a route laravel?

user9964622 :

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 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' => 'SettingController@code']);

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?

Hafez Divandari :

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)
{
    //...   
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=32990&siteId=1