【发布时间】:2021-02-07 06:50:57
【问题描述】:
当我在 route.php 文件中使用 laravel Route::resource 函数时,我正在使用 laravel 5,我可以在我的方法的参数中获取我的模型集合,如下所示:
//**web.php** file
Route::resource('factors', 'FactorsController');
//called url localhost:8000/factors/1/edit
//**FactorsController**
/**
* @param Request $request
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function edit(Request $request, Factor $factor)
{
//$factor is a collection of Factor Model that contains id '1' information in factor table
return view('factors.edit', compact('factor'));
}
这是正确的,但当我制作这样的自定义网址时:
Route::get('factors/{id}/newEdit', 'FactorsController@newEdit');
我无法在方法参数中获取集合,它返回空集合,如下所示:
//called url localhost:8000/factors/1/newEdit
1)
//**FactorsController**
/**
* @param Request $request
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function newEdit(Request $request, Factor $factor)
{
return view('factors.newEdit', compact('factor'));
}
$factor 是Factor Model 的空集合,但我希望我在数据库中选择的行。当我像这样使用它时,它是正确的:
2)
//**FactorsController**
/**
* @param Request $request
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function newEdit(Request $request, $id)
{
$factor = Factor::find($id);
return view('factors.newEdit', compact('factor'));
}
但我不想叫它像 2 我想叫它像 1
感谢您的帮助。
【问题讨论】: