Skip to content
Advertisement

Laravel 8: Argument 2 passed must be an instance of

I’m working with Laravel 8 to develop an Online Forum. And I made this form for updating an answer of a user:

<form action="{{ route('update.answer', [$anss->id, $que->id]) }}" method="POST">
    @csrf
    <textarea name="answer" id="answer" class="form-control" rows="7">{{ $ans->answer }}</textarea>
    @error('answer')
        <div class="text-red-500 mt-2 text-sm">
            {{ $message }}
        </div>
    @enderror
    <button type="submit" class="text-blue-500 BJadidBold">ثبت تفییرات</button>
</form>

And here is the route for this:

Route::post('questions/{ans}/{que}' , [QuestionController::class, 'updateAnswer'])->name('update.answer');

And this is also the Method of QuestionController:

public function updateAnswer(Answer $anss, Question $que)
    {
        $validate_data = Validator::make(request()->all(),[
           'answer' => 'required'
        ])->validated();

        $answer = Answer::findOrFail($anss);
        $answer->update($validate_data);
        return view('questions.question',[
            'show' => $que,
        ]);
    }

But now the problem is, whenever I submit the form in order to update answer, I get this as error:

Argument 2 passed to AppHttpControllersQuestionController::updateAnswer() must be an instance of AppModelsQuestion, string given, called in F:xampphtdocsgooyanetrootvendorlaravelframeworksrcIlluminateRoutingController.php on line 54

I don’t why I get this error, because as you can see I have passed $que->id at Action to Question $que as the 2nd parameter of udpateAnswer() method.

So what is your idea about this? How can I solve this issue?

I would really appreciate if you share your idea or suggestion with me.

Thanks…

Advertisement

Answer

It is because your variable in the controller is called $anss and in your route is called ans

Check this answer I have given in another topic.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement