【问题标题】:Laravel resource controller confusing workflowsLaravel 资源控制器混淆工作流程
【发布时间】:2021-04-05 23:47:22
【问题描述】:

我使用以下命令创建了多个资源控制器: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);
}

我的错误在哪里?

【问题讨论】:

  • 您能否显示返回新实例的操作的控制器方法签名
  • @lagbox 谢谢,我正在关注 Laravel 资源文档link

标签: laravel model controller resources


【解决方案1】:

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') }}

【讨论】:

  • 谢谢,它有效。但是函数签名是由 laravel artisan 命令 `php artisan make:model modelName -a' 自动生成的。是我的错吗?请推荐我
  • 函数签名由php artisan make:model ModelName -a 创建,但您在web.php 文件中创建了route,它不会自动为您生成。如果你想在你的函数中保留$companyIof,你可以大写你的路线。
猜你喜欢
  • 1970-01-01
  • 2014-05-29
  • 1970-01-01
  • 1970-01-01
  • 2017-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多