Route::get('{exam}/{practise}/{question}', [QuestionController::class,'create'])
public function create(Exam $exam, Practise $practise, Question $question)
{
dd($practise);
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi Devs,
I have a Question Model that belongs to Either or Practice Or Exam Model
The issue is I want to use one endpoint for creating both Practice and Exam Questions.
My current setup is currently returning a 404
Routes
Route::resource('{exam?}/{practise?}/question', QuestionController::class);
Controller
public function create(Exam $exam, Practise $practise)
{
dd($practise);
}
The above is returning a 404 but for example, if I remove the exam variable in the routes it works
Route::resource('{practise?}/question', QuestionController::class);
How can I solve this, just bear in mind the Question model belongs to many other parent models Like Quizzes, Courses, Tests, and so on. So creating a Controller and an endpoint for each relationship wouldn't make sense. Thank you.
@wakanda That’s not how optional parameters, nor resource routes, work. Optional parameters can also only be at the end of a route URI.
Route–model binding here isn’t going to work as Laravel is going to try and scope the question to the previous parameter. So if the question isn’t directly related to the previous parameter, the route binding’s going to fail.
You’ve not shown us what your actual models look like, but if a question can belong to either a practice or an exam, then that’s a clear candidate for a polymorphic relation. You would then need two resource routes:
Route::resource('exam.question', ExamQuestionController::class);
Route::resource('practice.question', PracticeQuestionController::class);
Please or to participate in this conversation.