Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Maximus1's avatar

How to get route url id

I need help getting the url id so as to use it for storage in a new table as homework id.

public function store(Request $request)
    {
        $homeworkanswer = new \App\HomeworkAnswer();
        $homeworkanswer->date = $request->date;
        $homeworkanswer->student_id = get_studentid();
        $homeworkanswer->homework_id = $request->route_id;
        $homeworkanswer->myanswer = $request->myanswer;
        
        
        $homeworkanswer->save();

This is the route :

Route::post('my_homework/store', 'Users\HomeworkController@store');

So when i am saving, it should automatically save the url id as homework id :

$homeworkanswer->homework_id = $request->route_id;

My method above returns nothing, how can i make it work?

0 likes
6 replies
siangboon's avatar

i don't recall that $request have aroute_id property.... I think you have to have an input named "route_id" with value then only can return value....

however, i don't think it's necessary to store route_id as homework_id but should related to your homeworks table if you have that.

Maximus1's avatar

Thank you siangboon but you did not address my question properly. First, i do not want an input name "route_id". I know how to do this if i want to but i want the route id from the url ---http://127.0.0.1/my_homework/add_answer/9.....I want to do this right in the controller. The id here for example is 9. I want it stored as homework_id when saving my homework as a student automatically.

Please note that the route_id is same as homework_id. The student answers are stored in an homeworkanswers table which as homework_id as a column as you can see here in the controller :

public function store(Request $request)
    {
        $homeworkanswer = new \App\HomeworkAnswer();
        $homeworkanswer->date = $request->date;
        $homeworkanswer->student_id = get_studentid();
        $homeworkanswer->homework_id = $request->route_id;
        $homeworkanswer->myanswer = $request->myanswer;
        
        
        $homeworkanswer->save();
himanshurajvanshi's avatar

If your url is like this http://127.0.0.1/my_homework/add_answer/9.

you have to add this into your route file like ->Route::Post('/add_answer/{id}','ControllerName@store;);

or into your controller you have to get id like this->

public function store(Request $request,$route_id) { $homeworkanswer = new \App\HomeworkAnswer(); $homeworkanswer->date = $request->date; $homeworkanswer->student_id = get_studentid(); $homeworkanswer->homework_id = $route_id; $homeworkanswer->myanswer = $request->myanswer; $homeworkanswer->save(); }

Maximus1's avatar

@himanshurajvanshi your solution did not work.

public function store(Request $request,$route_id)
    {
        $homeworkanswer = new \App\HomeworkAnswer();
        $homeworkanswer->date = $request->date;
        $homeworkanswer->student_id = get_studentid();
        $homeworkanswer->homework_id = $route_id;
        $homeworkanswer->myanswer = $request->myanswer;
        
        
        $homeworkanswer->save();

it gave this error - "Too few arguments to function App\Http\Controllers\Users\StudentController::store_assignmentanswer(), 1 passed and exactly 2 expected"

munazzil's avatar

Just check with below,

     public function store(Request $request,$homework_id )

else check below command with cmd,

     php artisan route:list

Please or to participate in this conversation.