Laravel 5 Route with multiple ID’s

冷暖自知 提交于 2019-12-11 11:52:29

问题


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:

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

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

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

I’m sorry if this question sounds stupid or is not even possible. I’m still very new to this.

Many thanks.


回答1:


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




回答2:


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




回答3:


     Route::get('export-employee-column/{key}/{header}','EmployeeManager\EmployeeTableDetailController@exportEmployeeColumn')->name('exportEmployeeColumn');

Export to Excel



来源:https://stackoverflow.com/questions/33939755/laravel-5-route-with-multiple-id-s

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!