我们知道 Laravel 8 及更高版本在其 routes 上有一个隐式 Binding 函数。我试图创建一个模型名称为 QuestionHelper
的控制器资源。当我使用 Route 资源定义路由时,如下所示
use App\Http\Controllers\QuestionHelperController;
Route::resource('questionhelper', QuestionHelperController::class);
不幸的是,隐式 binding 函数不适用于 show、edit、update 和 delete 方法。例如,当我访问显示页面时,隐式 binding 中的数据为空。
// When I access this method, the $questionHelper variable is empty
public function show(QuestionHelper $questionHelper)
{
return view('questionhelper.show', compact('questionHelper'));
}
或者当我使用编辑页面中包含的更新方法访问编辑方法时显示错误。
Illuminate\Routing\Exceptions\UrlGenerationException
Missing required parameter for [Route: questionhelper.update] [URI: admin/questionhelper/{questionhelper}] [Missing parameter: questionhelper]. (View: /var/www/resources/views/questionhelper/edit.blade.php)
为什么会这样?而在其他模型中,例如只有一个词的类别,效果很好。
谢谢你。
回答1
/**
* $parameter, is $id;
*
**/
<a href="{{ route('questionhelper.update', $parameter)}}"></a>
回答2
我解决了。
如果模型名称超过 2 个单词(在这种情况下为 QuestionHelper
),那么基本上我们需要将资源名称命名为 questionHelper
而不是 questionhelper
嗯我以前不知道