這可能是一個初學者問題,但在我的Laravel應用程序中有一些我不太明白的地方。 我在路由上使用id,我試圖讓laravel自動獲取數據庫中的對應對象。它非常適用於某些型號: 在路由:Laravel路由自動解析參數
Route::get('/project/{project}', '[email protected]');
,並在控制器
public function useProject(Request $request, Project $project)
之後,我有我的 「項目」 獲取從DB
protected 'attributes' =>
array (size=2)
'id' => int 9
'name' => string 'test' (length=4)
...
public 'exists' => boolean true
在代碼的另一部分,我在其他模型上使用相同的路由結構時,自動解析似乎不會發生
路由:
Route::get('/build/{testscenario}','[email protected]');
控制器:
public function create(Request $request, TestScenario $scenario){
而結果:
protected 'attributes' =>
array (size=0)
empty
...
public 'exists' => boolean false
兩款車型使用的是 「ID」 列作爲主鍵。 我錯過了什麼?
非常感謝您的幫助!
你是否試圖做一個路由模型綁定?參考這個例子https://scotch.io/tutorials/cleaner-laravel-controllers-with-route-model-binding –