无法从路由获取请求的数据



Laravel 8, PHP 7.4.3
尝试从路由获取请求ID。在本例中,我想从路由
中获得52
作为一个基本问题,仍然给我制造问题。
My Routehttp://127.0.0.1:8000用户/费用/52/编辑
我的代码
ExpenseController.php

// namespace AppHttpControllers;
public function edit(Request $request, Expense $expense)
{
//I tried running each dd individually.
dd( $request->get('id') ); //returns null
dd( $request->id ); //returns null
dd( $request['id'] ); // returns null
}

关于此控制器的参考,请检查此
请注意路由是由route::resource生成的在web.php文件中的函数。
我想知道如何从路由中获取ID。
我也尝试使用自定义表单请求但还是失败了,因为id为空
idrequest。php

//namespace AppHttpRequests;
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'id' => 'required'
];
}

这种方法也失败了。请告诉我,我错过了什么。IDRequest链接
感谢大家的帮助和提示。

// namespace AppHttpControllers;
public function edit(Request $request, Expense $expense)
{
}

$request->get('id')在表单数据或参数中查找id参数。

费用$费用将根据路由值加载主键id为52的资源。您可以从$expense变量中获取id。

最新更新