The Dead Man
The Dead Man

Reputation: 5576

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

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?

Upvotes: 1

Views: 2230

Answers (2)

Chinmay Mishra
Chinmay Mishra

Reputation: 1

I got my solution like this:

Route::match(["get", "post"], "/patient-member-orders/**{patient_id}/{member_id}**", [PathologyController::class, "patientMemberOrders"])->name("pathology-patient-member-orders");

After that we need to pass that name in our route method's second argument as an array in the Key Value pair.

**route("pathology-patient-member-orders", ["patient_id" => $member->patient_id, "member_id" => $member->id])**

Please correct me if I am wrong.

Chinmay Mishra

Upvotes: 0

Hafez Divandari
Hafez Divandari

Reputation: 9069

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

Upvotes: 8

Related Questions