在使用-mcr(php artisan make:model Institution -mrc)创建模型后, Controller 中的show函数搭建为:
/**
* Display the specified resource.
*
* @param \App\Organization\Institution $institution
* @return \Illuminate\Http\Response
*/
public function show(Institution $institution)
{
return view('institutions.show', ['institution' => $institution]);
}
return view... 是我插入的。我期待它用在参数中发送了 id 的对象填充它。
/institutions/1
但是,在使用 dd($institution) 之后,我验证了它具有 ID,而不是对象。
这个变量不应该返回对象吗?
最佳答案
这叫做 Route Model Binding .您的路线需要类似于:
Route::get('institutions/{institution}', 'InstitutionController@show');
然后根据你的 Controller
public function show(Institution $institution)
{
return view('institutions.show', compact($institution))
}
您可以阅读更多关于此 here 的信息.
我想你的路由有一个名为 {id}
的参数,而不是 {institution}
。
关于php - Laravel Controller 类型提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51748388/