Support the ongoing development of Laravel.io →
Requests Input Eloquent
Last updated 9 months ago.
0

Hey,

I think you are looking for something like this

Route::group(array('prefix' => 'student'), function()
{
    Route::get('/', function()
    {
       return 'Student Home Page';
    });
    Route::get('{student_id?}', function($student_id)
    {
        return $student_id;
    })->where('student_id', '[0-9]+');//Allows only digits
    Route::get('{student_id?}/report', function()
    {
        return 'Report home page';
    })->where('student_id', '[0-9]+');//Allows only digits
    Route::get('{student_id?}/report/{report_id?}', function($student_id, $report_id)
    {
        return 'Student id is : ' . $student_id . ' and report id is : ' . $report_id;
    })->where(array('student_id' => '[0-9]+', 'report_id' => '[0-9]+'));//Allows only digits
});
Last updated 9 months ago.
0

evandertino said:

Hey,

I think you are looking for something like this

Route::group(array('prefix' => 'student'), function()
{
   Route::get('/', function()
   {
      return 'Student Home Page';
   });
   Route::get('{student_id?}', function($student_id)
   {
       return $student_id;
   })->where('student_id', '[0-9]+');//Allows only digits
   Route::get('{student_id?}/report', function()
   {
       return 'Report home page';
   })->where('student_id', '[0-9]+');//Allows only digits
   Route::get('{student_id?}/report/{report_id?}', function($student_id, $report_id)
   {
       return 'Student id is : ' . $student_id . ' and report id is : ' . $report_id;
   })->where(array('student_id' => '[0-9]+', 'report_id' => '[0-9]+'));//Allows only digits
});

Couple of questions.

  1. Why are the student_id's optional?

  2. Why not just define a pattern for student_id and drop the where clauses? Route::pattern('student_id', '[0-9]+');

Last updated 9 months ago.
0

Hi Evandertino,

I tried your solution and something similar to that previously and it didn't work.

My code in the first post works as here are the routes generated below:

 GET|HEAD student/{student_id}/report               | student.{student_id}.report.index   | [email protected]                  | auth           |               |
 |        | GET|HEAD student/{student_id}/report/create        | student.{student_id}.report.create  |    [email protected]       | auth           |               |
 |        | POST student/{student_id}/report                   | student.{student_id}.report.store   |   [email protected]        | auth           |               |
 |        | GET|HEAD student/{student_id}/report/{report}      | student.{student_id}.report.show    | [email protected]         | auth           |               |
 |        | GET|HEAD student/{student_id}/report/{report}/edit | student.{student_id}.report.edit    | [email protected]         | auth           |               |
 |        | PUT student/{student_id}/report/{report}           | student.{student_id}.report.update  | [email protected]       | auth           |               |
 |        | PATCH student/{student_id}/report/{report}         |                                     | [email protected]       | auth           |               |
 |        | DELETE student/{student_id}/report/{report}        | student.{student_id}.report.destroy | [email protected]      | auth           |               |

The issue is that for the student.{student_id}.report.show route, instead of showing a report with the report id (provided in url), it shows with the student id.

So to clarify, it is picking up the student_id for the [email protected], which is the wrong ID.

Last updated 9 months ago.
0

imdadahad

Aha, Now i understand your question. You are trying to achieve your functionality using REST.

You should eliminate the group prefix, to get readable routes this way.

Route::resource('student/{student_id}/report', 'ReportRecordController');

Or you can use your previous code

Route::group(array('prefix' => 'student/{student_id}/'), function()
{
    Route::resource('report', 'ReportRecordController');

});

But then to extract the ids independently from the controller you have to do this

ReportRecordController.php

	public function show($student_id, $report_id)
	{
		return $student_id . $report_id;
	}
Last updated 9 months ago.
0

evandertino,

Perfect! That's exactly what I was looking for and it works!

Appreciate all the help :)

Thanks

Last updated 9 months ago.
0

Sign in to participate in this thread!

Full Stack Europe

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2023 Laravel.io - All rights reserved.