Home » Php » php – Passing 2 Parameters to Laravel Routes – Resources

php – Passing 2 Parameters to Laravel Routes – Resources

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m trying to build my routes using resources so that I can pass two parameters into my resources.

I’ll give you a few examples of how the URLS would look:

domain.com/dashboard
domain.com/projects
domain.com/project/100
domain.com/project/100/emails
domain.com/project/100/email/3210
domain.com/project/100/files
domain.com/project/100/file/56968

So you can see I always need to have reference to the project_id and also the email/file id etc.

I realize I can do this manually by writing all routes by hand, but I’m trying to stick to the resource model.

I figured something like this might work?

Route::group(['prefix' => 'project'], function(){
  Route::group(['prefix' => '{project_id}'], function($project_id){

    // Files
    Route::resource('files', 'FileController');

  });
});
How to&Answers:

As far as I know about resources

Route::resource('files', 'FileController');

The above mentioned resource will route the following urls.

Few Actions Handled By Resource Controller for your Route::resource('files', 'FileController');

Route::get('files',[email protected]) // get req will be routed to the index() function in your controller
Route::get('files/{val}',[email protected]) // get req with val will be routed to the show() function in your controller
Route::post('files',[email protected]) // post req will be routed to the store() function in your controller
Route::put('files/{id}',[email protected]) // put req with id will be routed to the update() function in your controller
Route::delete('files',[email protected]) // delete req will be routed to the destroy() function in your controller

the single resource Mentioned above will do all the listed routing

Apart from those you have to write your custom route

In your scenario of

Route::group(['prefix' => 'project'], function(){
  Route::group(['prefix' => '{project_id}'], function($project_id){

    // Files
    Route::resource('files', 'FileController');

  });
}); 

domain.com/project/100/files

if its a get request will be routed to [email protected]
if its a post request will be routed to [email protected]

if your “domain.com/project/100/file/56968” is changed to “domain.com/project/100/files/56968 (file to files)then the following rooting will occur…

domain.com/project/100/files/56968

if its a get request will be routed to [email protected]
if its a put request will be routed to [email protected]
if its a delete request will be routed to [email protected]

and it has no impact on any other urls you have mentioned

Provided, you need to have RESTful Resource Controllers

Answer:

For the request like ‘/project/100/file/56968’, you must specify your route like this:

Route::resource('project.file', 'FileController');

And then you can get parameters at the show method of the controller:

public function show($project, $file) {
    dd([
        '$project' => $project,
        '$file' => $file
    ]);
}

The result of this example will be:

array:2 [▼
  "$project" => "100"
  "$file" => "56968"
]