Laravel 8:缺少[路由:编辑问题][URI:编辑问题/{问题}][缺少参数:问题]的必需参数

2022-03-27 00:00:00 php laravel laravel-8

我正在使用Laravel 8开发我的论坛项目,在此项目中,我想创建一个editquestion页面,用户可以编辑他们的问题。

刀片式服务器上有一个按钮可将用户重定向到编辑页面:
<form action="{{ route('edit.question', $show->slug) }}">
   <button type="submit" class="text-blue-500">Edit Question</button>
</form>

这是显示editquestion刀片的路径:

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

这是控制器方法editQuestion(),它返回刀片:

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

但现在,每当我单击Edit Question时,我都会收到以下错误:

缺少[Route:edit.Quest][URI:editQuery/{Quest}][缺少参数:Query]

那么这里出了什么问题呢?我如何解决此问题?

如果您能就此分享您的想法或建议,我将不胜感激…

提前感谢。

更新#1:

我添加了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>

并收到此错误:

Missing required parameter for [Route: edit.question] [URI: editquestion/{slug}] [Missing parameter: slug]. (View: F:xampphtdocsgooyanetootesourcesviewsquestionsquestion.blade.php)

更新#2:

这里是web.php路由:

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');
});

解决方案

发件人

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

您正在editQuery(路由-模型绑定)中注入Question model,因此您还应该在窗体中传递问题类实例。

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

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

应该可以正常工作。

相关文章