Laravel 资源控制器混乱的工作流程
Laravel resource controller confusing workflows
我使用以下命令创建了多个资源控制器:php artisan make:model MobelName -a。有一些模型和控制器列表:
型号列表:
Iof
CompanyIof
Company
Test
控制器列表:
IofController
CompanyIofController
CompanyController
TestController
问题是 edit() 方法仅适用于 Company 和 Test 模型。不适用于 Iof 和 CompanyIof 模型
找到的结果:
App\Company {#1379 ▼
#guarded: []
#connection: "mysql"
#table: "companies"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:10 [▼
"id" => 1
"type" => "1"
"precedence" => 22
"systemId" => 23
"shortName" => "shah alasmasdljk"
"fullName" => "dsdaf"
"status" => "1"
"user_id" => 1
"created_at" => "2020-12-29 13:58:19"
"updated_at" => "2020-12-29 13:58:19"
]
#original: array:10 [▼
"id" => 1
"type" => "1"
"precedence" => 22
"systemId" => 23
"shortName" => "shah alasmasdljk"
"fullName" => "dsdaf"
"status" => "1"
"user_id" => 1
"created_at" => "2020-12-29 13:58:19"
"updated_at" => "2020-12-29 13:58:19"
]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
}
未找到结果:
App\CompanyIof {#1367 ▼
#guarded: []
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#attributes: []
#original: []
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
}
路线:
Route::resource('/iof','IofController');
Route::resource('/companyiof','CompanyIofController');
Route::resource('/company','CompanyController');
Route::resource('/test','TestController');
像这样路由动作-
{{ url('iof/'.$id.'/edit') }}
{{ url('companyiof/'.$id.'/edit') }}
{{ url('company/'.$id.'/edit') }}
{{ url('test/'.$id.'/edit') }}
资源控制器edit()方法:
public function edit(Iof $iof)
{
dd($iof);
}
public function edit(CompanyIof $companyIof)
{
dd($companyIof);
}
public function edit(Company $company)
{
dd($company);
}
public function edit(Test $test)
{
dd($test);
}
我的错误在哪里?
您的 CompanyIof
路由模型绑定的字符大小写似乎不匹配。
您已经使用全部小写字母定义了 route resource
,但在您的函数签名中使用了驼峰式字母。这些需要匹配才能使路由模型绑定正常工作。
Route::resource('companyiof', CompanyIofController::class);
public function edit(CompanyIof $companyiof)
{
dd($companyiof);
}
更新
如果您希望函数中的变量名与使用 php artisan make:model CompanyIof -a
时函数签名中自动生成的变量名相同,那么只需更新您的 route
:
Route::resource('companyIof', CompanyIofController::class);
public function edit(CompanyIof $companyIof)
{
dd($companyIof);
}
不过您需要更新 url
细分:
{{ url('companyIof/'.$id.'/edit') }}
我使用以下命令创建了多个资源控制器:php artisan make:model MobelName -a。有一些模型和控制器列表:
型号列表:
Iof
CompanyIof
Company
Test
控制器列表:
IofController
CompanyIofController
CompanyController
TestController
问题是 edit() 方法仅适用于 Company 和 Test 模型。不适用于 Iof 和 CompanyIof 模型
找到的结果:
App\Company {#1379 ▼
#guarded: []
#connection: "mysql"
#table: "companies"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:10 [▼
"id" => 1
"type" => "1"
"precedence" => 22
"systemId" => 23
"shortName" => "shah alasmasdljk"
"fullName" => "dsdaf"
"status" => "1"
"user_id" => 1
"created_at" => "2020-12-29 13:58:19"
"updated_at" => "2020-12-29 13:58:19"
]
#original: array:10 [▼
"id" => 1
"type" => "1"
"precedence" => 22
"systemId" => 23
"shortName" => "shah alasmasdljk"
"fullName" => "dsdaf"
"status" => "1"
"user_id" => 1
"created_at" => "2020-12-29 13:58:19"
"updated_at" => "2020-12-29 13:58:19"
]
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
}
未找到结果:
App\CompanyIof {#1367 ▼
#guarded: []
#connection: null
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: false
+wasRecentlyCreated: false
#attributes: []
#original: []
#changes: []
#casts: []
#classCastCache: []
#dates: []
#dateFormat: null
#appends: []
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#hidden: []
#visible: []
#fillable: []
}
路线:
Route::resource('/iof','IofController');
Route::resource('/companyiof','CompanyIofController');
Route::resource('/company','CompanyController');
Route::resource('/test','TestController');
像这样路由动作-
{{ url('iof/'.$id.'/edit') }}
{{ url('companyiof/'.$id.'/edit') }}
{{ url('company/'.$id.'/edit') }}
{{ url('test/'.$id.'/edit') }}
资源控制器edit()方法:
public function edit(Iof $iof)
{
dd($iof);
}
public function edit(CompanyIof $companyIof)
{
dd($companyIof);
}
public function edit(Company $company)
{
dd($company);
}
public function edit(Test $test)
{
dd($test);
}
我的错误在哪里?
您的 CompanyIof
路由模型绑定的字符大小写似乎不匹配。
您已经使用全部小写字母定义了 route resource
,但在您的函数签名中使用了驼峰式字母。这些需要匹配才能使路由模型绑定正常工作。
Route::resource('companyiof', CompanyIofController::class);
public function edit(CompanyIof $companyiof)
{
dd($companyiof);
}
更新
如果您希望函数中的变量名与使用 php artisan make:model CompanyIof -a
时函数签名中自动生成的变量名相同,那么只需更新您的 route
:
Route::resource('companyIof', CompanyIofController::class);
public function edit(CompanyIof $companyIof)
{
dd($companyIof);
}
不过您需要更新 url
细分:
{{ url('companyIof/'.$id.'/edit') }}