php - Laravel Controller 类型提示

标签 php laravel laravel-5.6 type-hinting

在使用-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/

相关文章:

php - 从 Android 应用程序到 mysql 数据库的阿拉伯文本

mysql - 如何在 Laravel Eloquent 地解决对 json 数据的 case-sensitive where like 语句?

php - Laravel 5.6 不同函数返回同一 View

php - 如何在 Laravel 中向模型添加自定义字段?

php - Mysql保存12月31日文章14 :25 but data function showing the date in 2019

laravel - Eloquent 的 Model::query() 是什么意思?

php - firephp 输出中的彩色点是什么意思?

php - 我有一个充满 <a> 标签的 mySQL 数据库,但在 echo'ing 时链接不起作用

php - 链接正确时未找到样式表

laravel - 给出异常 : trim epects parameter 1 to be string, 数组