gpt4 book ai didi

php - Laravel 没有将模型加载到路由中

转载 作者:行者123 更新时间:2023-12-05 09:18:28 26 4
gpt4 key购买 nike

我目前正在开发一个 PHP Laravel 应用程序,到目前为止一切进展顺利。但是,我最新的名为 FreightRequest 的资源导致了我无法弄清楚的极其奇怪的行为。

我的路由文件有以下内容:

Route::resource('freightrequests', 'FreightRequestController');

当我访问 http://localhost/freightrequests/1 时,laravel 没有正确地将模型绑定(bind)到我的 FreightRequestController@show 方法。

我的方法是这样的:

public function show(FreightRequest $freightRequest)
{
dd($freightRequest);

return view('freightrequests.show', compact('freightRequest'));
}

上面的结果在下面的转储中:

FreightRequest {#429 ▼
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#attributes: []
#original: []
#casts: []
#dates: []
#dateFormat: null
#appends: []
#events: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
#guarded: array:1 [▶]
}

事实上,如果我转到一个不存在的 ID http://localhost/freightrequests/12343243,Laravel 不会抛出 NotFoundHttpException。如果我访问 http://localhost/users/12343243,我会得到一个 NotFoundHttpException。除了这个资源,我的所有其他资源都工作正常。

我什至试图放弃 Route::resource 并将其替换为:

Route::get('freightrequests/{freightrequest}', 'FreightRequestController@show')->name('freightrequests.index');

我已经尝试重命名模型、 Controller 、路由和所有内容,以防出现冲突导致 Laravel 无法检索模型。它似乎注入(inject)了正确的类实例,但实际上并没有尝试从服务器获取它。我试过清除缓存。我完全无法弄清楚为什么这个特定模型无法正常工作,而我的其他 6 个左右的资源却可以正常工作。

有什么想法吗?

最佳答案

我在发布问题后不久就发现了问题:

public function show(FreightRequest $freightRequest)
{
dd($freightRequest);

return view('freightrequests.show', compact('freightRequest'));
}

应该是:

public function show(FreightRequest $freightrequest)
{
dd($freightrequest);

return view('freightrequests.show', compact('freightrequest'));
}

尝试绑定(bind)参数时区分大小写很重要,我花了很长时间才意识到 R 是大写的:)

关于php - Laravel 没有将模型加载到路由中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44339691/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com