0w3n86
0w3n86

Reputation: 519

Laravel 5 Route with multiple ID’s

I would like to pass two sets of ID’s into a route.

The first ID is that of the authenticated user so for example 3 and the second is that of a candidate.

So what I would like it to do is: vault/3/candidates/120

Here is the route I want to have:

Route::get('vault/{id}/candidates/{id}', 'CandidateController@centreCandidatesShow');

Using:

public function centreCandidatesShow($id)
{
    $candidate = Candidate::with('qualification')->find($id);

    return view('vault.show', compact('candidate'));
}

Could someone let me know if this is possible, and if so, how?

Upvotes: 1

Views: 11824

Answers (2)

migsAV
migsAV

Reputation: 291

You need to put the ids in an array

Route::get('vault/{userId}/candidates/{candidateId}', [
    'as' => 'candidates.show', 
    'uses' => 'CandidateController@centreCandidatesShow'
]);

<a href="{{ route('candidates.show', ['userId'=$userId, 'candidateId'=$candidateId]) }}">Link to candidate</a>

laravel 5 route passing two parameters

Upvotes: 0

Pistachio
Pistachio

Reputation: 1652

routes.php:

Route::get('vault/{userId}/candidates/{candidateId}', 'CandidateController@centreCandidatesShow');

CandidatesController.php:

public function centreCandidatesShow($userId, $canidateId)
{
    $candidate = Candidate::with('qualification')->find($canidateId);
    $user = User::find($userId);

    return view('vault.show', compact('candidate'));
}

Named routes

I highly recommend using named routes.

Route::get('vault/{userId}/candidates/{candidateId}', [
    'as' => 'candidates.show', 
    'uses' => 'CandidateController@centreCandidatesShow'
]);

This will not only help you generate url's but also helps when passing parameters!

Example:

<a href="{{ route('candidates.show', $userId, $candidateId) }}">Link to candidate</a>

This will provide the link and pass in the parameters!

You can even redirect to a route from your controller!

return redirect()->route('candidates.show', $userId, $candidateId);


Explanation:

You can put whatver you want as route parameters. Anything inside curly brackets are considered a valid parameter. Another example would be:

Route::get('country/{country}/city/{city}/street/{street}/zip{zip}', 'AddressController@show');

In your AddressController@show you would accept these parameters, in order.

public function show($country, $city, $street, $zip) {..}

Docs: http://laravel.com/docs/5.1/routing#route-parameters

Upvotes: 7

Related Questions