将记录保存到数据库Laravel 10

mwg9r5ms  于 2023-05-23  发布在  其他
关注(0)|答案(2)|浏览(328)

我正在尝试保存表单中的数据。但是,当我检查数据库端时,没有任何记录。
我使用Laravel 10和mySQL v5.7。

***edit:我使用的是Azure MySQL Flexible Server v5.7。我在本地安装了一个MySQL,它保存得很好。这可能是Azure的问题。Thank you:)
控制器store()

  1. public function store(Request $request)
  2. {
  3. $personnelRegistry = null;
  4. $personnerlRegistryId = (int)$request->input('personnelRegistryId', null);
  5. if($personnerlRegistryId && $request->isMethod('post')){
  6. $personnelRegistry = PersonnelRegistry::find($personnerlRegistryId)->getAttributes();
  7. $joinDate = $request->get('joinDate');
  8. $personnel = new PersonnelDetail;
  9. $personnel->personnel_registry_id = $personnerlRegistryId;
  10. $personnel->service_id = $request->get('serviceId');
  11. $personnel->emp_status_id = (int)$request->get('employmentStatusId');
  12. $personnel->joining_date = date("Y-m-d", strtotime($joinDate));
  13. $personnel->ref_pos_id = (int)$request->get('posId');
  14. $personnel->step_id = (int)$request->get('stepId');
  15. $personnel->current_salary = 1;
  16. $personnel->is_current = (int)$request->get('isPersonnelCurrent');
  17. $personnel->created_by = 1;
  18. $personnel->updated_by = 1;
  19. $personnel->save();
  20. $request->session()->flash('status', 'Personnel details was created.');
  21. return redirect()->route('employee.show', ['employee' => $personnerlRegistryId]);
  22. } else {
  23. //Validation failed. Redirect
  24. }
  25. }

型号

  1. use Illuminate\Database\Eloquent\Factories\HasFactory;
  2. use Illuminate\Database\Eloquent\Model;
  3. class PersonnelDetail extends Model
  4. {
  5. use HasFactory;
  6. public $table = 'personnel_details';
  7. protected $primaryKey = 'id';
  8. /**
  9. *
  10. * @var bool
  11. */
  12. public $timestamps = true;
  13. }

查看

  1. <form action="{{ route('employee.store') }}" method="POST">
  2. @csrf
  3. <div class="mb-3 row">
  4. <label class="col-lg-2 col-md-3 col-sm-4 col-form-label">Personnel Name</label>
  5. <div class="col-sm-8 col-md-9 col-lg-10">
  6. <span id="personnelNamePlaceholder"></span>
  7. <input type="hidden" name="personnelRegistryId" id="personnelRegistryId" required />
  8. <button type="button" class="btn btn-outline-primary mb-1" data-bs-toggle="modal" data-bs-target="#searchPersonnel">Search Records</button>
  9. </div>
  10. </div>
  11. <div class="mb-3 row">
  12. <label class="col-lg-2 col-md-3 col-sm-4 col-form-label">Service Id</label>
  13. <div class="col-sm-8 col-md-9 col-lg-10">
  14. <input type="text" name="serviceId" class="form-control" required />
  15. </div>
  16. </div>
  17. <div class="mb-3 row">
  18. <label class="col-lg-2 col-md-3 col-sm-4 col-form-label">Employee Status Id</label>
  19. <div class="col-sm-8 col-md-9 col-lg-10">
  20. <select name="employmentStatusId" class="select2 form-control" id="employmentStatus">
  21. <option label="&nbsp;"></option>
  22. <option value="24">Permanent</option>
  23. <option value="25">Contractual</option>
  24. </select>
  25. </div>
  26. </div>
  27. <div class="mb-3 row">
  28. <label class="col-lg-2 col-md-3 col-sm-4 col-form-label">Join Date</label>
  29. <div class="col-sm-8 col-md-9 col-lg-10">
  30. <input type="text" name="joinDate" class="form-control date-picker-close" id="joindate" value="{{ $date_now }}">
  31. </div>
  32. </div>
  33. <div class="mb-3 row">
  34. <label class="col-lg-2 col-md-3 col-sm-4 col-form-label">Position</label>
  35. <div class="col-sm-8 col-md-9 col-lg-10">
  36. <select name="posId" class="form-control" id="positionId" require>
  37. <option label="&nbsp;"></option>
  38. @foreach($plantilla_items as $item)
  39. <option value="{{ $item->id }}">{{ $item->uniq_item_no }}</option>
  40. @endforeach
  41. </select>
  42. </div>
  43. </div>
  44. <div class="mb-3 row">
  45. <label class="col-lg-2 col-md-3 col-sm-4 col-form-label">Step</label>
  46. <div class="col-sm-8 col-md-9 col-lg-10">
  47. <select name="stepId" class="form-control" id="stepId" require>
  48. <option label="&nbsp;"></option>
  49. @foreach($steps as $step)
  50. <option value="{{ $step->id }}">{{ $step->description }}</option>
  51. @endforeach
  52. </select>
  53. </div>
  54. </div>
  55. <div class="mb-3 row">
  56. <label class="col-lg-2 col-md-3 col-sm-4 col-form-label">Is Current?</label>
  57. <div class="col-sm-8 col-md-9 col-lg-10">
  58. <div class="form-check form-check-inline">
  59. <input class="form-check-input" type="radio" name="isPersonnelCurrent" id="inlineRadio1" value="1" />
  60. <label class="form-check-label" for="inlineRadio1">Yes</label>
  61. </div>
  62. <div class="form-check form-check-inline">
  63. <input class="form-check-input" type="radio" name="isPersonnelCurrent" id="inlineRadio2" value="0" />
  64. <label class="form-check-label" for="inlineRadio2">No</label>
  65. </div>
  66. </div>
  67. </div>
  68. <div class="mb-3 row mt-5">
  69. <div class="col-sm-8 col-md-9 col-lg-10 ms-auto">
  70. <button type="submit" class="btn btn-outline-primary">Save</button>
  71. </div>
  72. </div>
  73. </form>

laravel没有错误,它成功重定向到employee.show页面。
请和谢谢你。
我希望在保存()之后数据库中有数据。
dd

创建表personnel_details

  1. CREATE TABLE `personnel_details` (
  2. `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  3. `personnel_registry_id` bigint(20) unsigned NOT NULL,
  4. `service_id` smallint(5) unsigned NOT NULL,
  5. `org_code_id` smallint(5) unsigned DEFAULT NULL,
  6. `position_id` smallint(5) unsigned DEFAULT NULL,
  7. `emp_status_id` smallint(5) unsigned NOT NULL,
  8. `joining_date` date NOT NULL,
  9. `leave_date` date DEFAULT NULL,
  10. `ref_pos_id` bigint(20) unsigned NOT NULL,
  11. `step_id` smallint(5) unsigned NOT NULL,
  12. `is_current` tinyint(3) unsigned NOT NULL,
  13. `current_salary` decimal(10,2) DEFAULT NULL,
  14. `created_by` bigint(20) unsigned DEFAULT NULL,
  15. `updated_by` bigint(20) unsigned DEFAULT NULL,
  16. `created_at` timestamp NULL DEFAULT NULL,
  17. `updated_at` timestamp NULL DEFAULT NULL,
  18. PRIMARY KEY (`id`),
  19. KEY `FKPersonnelRegistryTOPersonnelDetails_idx` (`personnel_registry_id`),
  20. CONSTRAINT `FKPersonnelRegistryTOPersonnelDDetails` FOREIGN KEY (`personnel_registry_id`) REFERENCES `personnel_registry` (id)
  21. ) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci'

创建表personnel_registry

  1. CREATE TABLE `personnel_registry` (
  2. `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  3. `agency_emp_id` bigint(20) unsigned NOT NULL,
  4. `last_name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  5. `first_name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  6. `middle_name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  7. `extension_name` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
  8. ...
  9. `created_by` bigint(20) unsigned NOT NULL,
  10. `updated_by` bigint(20) unsigned NOT NULL,
  11. `created_at` timestamp NULL DEFAULT NULL,
  12. `updated_at` timestamp NULL DEFAULT NULL,
  13. PRIMARY KEY (`id`)
  14. ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
qni6mghb

qni6mghb1#

必须在模型上定义可填充属性。像这样:

  1. protected $fillable = ['personnel_registry_id', 'service_id'];

或定义$guided为空属性。但考虑到这是风险

  1. protected $guarded = [];

如需详细信息,请参阅文档

vlju58qv

vlju58qv2#

您在模型初始化上缺少括号“()”标记。试试这个:

  1. public function store(Request $request) {
  2. $personnelRegistry = null;
  3. $personnerlRegistryId = (int)$request->input('personnelRegistryId');
  4. if($personnerlRegistryId && $request->isMethod('post')){
  5. $personnelRegistry = PersonnelRegistry::find($personnerlRegistryId)->getAttributes();
  6. $joinDate = $request->get('joinDate');
  7. $personnel = new PersonnelDetail();
  8. $personnel->personnel_registry_id = $personnerlRegistryId;
  9. $personnel->service_id = $request->get('serviceId');
  10. $personnel->emp_status_id = (int)$request->get('employmentStatusId');
  11. $personnel->joining_date = date("Y-m-d", strtotime($joinDate));
  12. $personnel->ref_pos_id = (int)$request->get('posId');
  13. $personnel->step_id = (int)$request->get('stepId');
  14. $personnel->current_salary = 1;
  15. $personnel->is_current = (int)$request->get('isPersonnelCurrent');
  16. $personnel->created_by = 1;
  17. $personnel->updated_by = 1;
  18. $personnel->save();
  19. $request->session()->flash('status', 'Personnel details was created.');
  20. return redirect()->route('employee.show', ['employee' => $personnerlRegistryId]);
  21. } else {
  22. //Validation failed. Redirect
  23. }
  24. }

不同的是

  1. $personnel = new PersonnelDetail;

我把它改成:

  1. $personnel = new PersonnelDetail();

在Laravel中,当你想在一个控制器中示例化一个模型时,你需要在模型名称后面使用圆括号(),因为它代表一个方法调用。模型名称后面跟括号表示您正在调用模型类的构造函数来创建该模型的新示例。
通过包含括号,您调用了构造函数方法,该方法负责设置模型对象的初始状态并准备使用。它允许您创建模型类的新示例,并在控制器中访问其属性和方法。

展开查看全部

相关问题