Laravel 8: Missing required parameter for [Route: edit.question] [URI: editquestion/{question}] [Missing parameter: question]

All we need is an easy explanation of the problem, so here it is.

I’m working with Laravel 8 to develop my forum project and in this project, I want to make an editquestion page that users can edit their questions.

So here is a button on blade that redirects user to that edit page:

<form action="{{ route('edit.question', $show->slug) }}">
   <button type="submit" class="text-blue-500">Edit Question</button>
</form>

And this is the route for showing the editquestion blade:

Route::get('editquestion/{question:slug}' , [QuestionController::class, 'editQuestion'])->name('edit.question');

And this is the Controller method editQuestion() which returns a blade:

public function editQuestion(Question $slug)
    {
        return view('questions.editquestion',[
            'slug' => $slug
        ]);
    }

But now, whenever I click on Edit Question, I get this error:

Missing required parameter for [Route: edit.question] [URI: editquestion/{question}] [Missing parameter: question]

So what is going wrong here? How can I solve this issue?

I would really appreciate if you share your idea or suggestion about this…

Thanks in advance.

UPDATE #1:

I added input hidden of slug:

<form action="{{ route('edit.question') }}" method="POST">
   @csrf
   <input type="hidden" value="{{ $show->slug }}" />
   <button type="submit" class="text-blue-500">Edit Question</button>
</form>

And get this error:

Missing required parameter for [Route: edit.question] [URI: editquestion/{slug}] [Missing parameter: slug]. (View: F:\xampp\htdocs\gooyanet\root\resources\views\questions\question.blade.php)

UPDATE #2:

Here is web.php routes:

Route::middleware('auth')->group(function() {
    Route::get('logout', [LoginController::class, 'logout']);
    Route::resource('profile' , ProfileController::class);
    Route::get('ask' , [QuestionController::class, 'showForm'])->name('ask');
    Route::post('ask' , [QuestionController::class, 'postForm']);
    Route::get('questions/{slug}' , [QuestionController::class, 'showQuestion']);
    Route::post('questions/{question}/answer' , [QuestionController::class, 'postAnswer'])->name('questions.answers');
    Route::get('answers/{ans}' , [QuestionController::class, 'editAnswer'])->name('edit.answer');
    Route::get('editquestion/{slug}' , [QuestionController::class, 'editQuestion'])->name('edit.question');
    Route::post('editquestion/{id}' , [QuestionController::class, 'updateQuestion'])->name('update.question');
    Route::post('questions/{ans}' , [QuestionController::class, 'updateAnswer'])->name('update.answer');
    Route::delete('questions/{ans}' , [QuestionController::class, 'destroyAnswer'])->name('destroy.answer');
    Route::delete('{id}' , [QuestionController::class, 'destroyQuestion'])->name('destroy.question');
});

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

From

public function editQuestion(Question $slug)
    {
        return view('questions.editquestion',[
            'slug' => $slug
        ]);
    }

you are injecting Question model in editQuestion(Route-model binding), so you should pass your question class instance in your form too.

<form action="{{ route('edit.question', $show) }}">
   <button type="submit" class="text-blue-500">Edit Question</button>
</form>

or

<form action="{{ route('edit.question', ['question' => $show]) }}">

should work fine.

Method 2

The first problem notice is incorrect syntax in Blade (options parameters should be an array in the route() function with parameter key name same as you define it in route declaration) :
Change this :

<form action="{{ route('edit.question', $show->slug) }}">

to this :

<form action="{{ route('edit.question', ['question' => $show->slug]) }}">

The other thing I think is incorrect syntax also in routing (route parameter should be only one key word, you have to choose beetween "question" or "slug", not both separated with dots) :

Route::get('editquestion/{question}' , [QuestionController::class, 'editQuestion'])->name('edit.question');

If you want to make this route parameter optional see : https://laravel.com/docs/8.x/routing#parameters-optional-parameters

Also, i don’t know what your object $show->slug contain, but you should check it with a dump.

Method 3

Just add slug parameter to the route function

<form action="{{ route('edit.question', ['slug' => $show->slug]) }}">

Method 4

Sometimes, it happens because you haven’t set the primary key and auto-increment. Please check the database and confirm your primary key column is not null.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply