将记录保存到数据库Laravel 10

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

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

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

public function store(Request $request)
    {
        $personnelRegistry = null;
        $personnerlRegistryId = (int)$request->input('personnelRegistryId', null);

        if($personnerlRegistryId && $request->isMethod('post')){
            $personnelRegistry = PersonnelRegistry::find($personnerlRegistryId)->getAttributes();
            $joinDate = $request->get('joinDate');

                $personnel = new PersonnelDetail;
                $personnel->personnel_registry_id = $personnerlRegistryId;
                $personnel->service_id = $request->get('serviceId');
                $personnel->emp_status_id = (int)$request->get('employmentStatusId');
                $personnel->joining_date = date("Y-m-d", strtotime($joinDate));
                $personnel->ref_pos_id = (int)$request->get('posId');
                $personnel->step_id = (int)$request->get('stepId');
                $personnel->current_salary = 1;
                $personnel->is_current = (int)$request->get('isPersonnelCurrent');
                $personnel->created_by = 1;
                $personnel->updated_by = 1;

                $personnel->save();

            $request->session()->flash('status', 'Personnel details was created.');

            return redirect()->route('employee.show', ['employee' => $personnerlRegistryId]);
        } else {
           //Validation failed. Redirect
        }
    }

型号

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class PersonnelDetail extends Model
{
    use HasFactory;

    public $table = 'personnel_details';
    protected $primaryKey = 'id';

    /**
     *
     * @var bool
     */
    public $timestamps = true;

}

查看

<form action="{{ route('employee.store') }}" method="POST">
                    @csrf
                    <div class="mb-3 row">
                        <label class="col-lg-2 col-md-3 col-sm-4 col-form-label">Personnel Name</label>
                        
                        <div class="col-sm-8 col-md-9 col-lg-10">

                            <span id="personnelNamePlaceholder"></span>
                            <input type="hidden" name="personnelRegistryId" id="personnelRegistryId" required />
                            <button type="button" class="btn btn-outline-primary mb-1" data-bs-toggle="modal" data-bs-target="#searchPersonnel">Search Records</button>
                        </div>
                    </div>

                    <div class="mb-3 row">
                        <label class="col-lg-2 col-md-3 col-sm-4 col-form-label">Service Id</label>
                        <div class="col-sm-8 col-md-9 col-lg-10">
                            <input type="text" name="serviceId" class="form-control" required />
                        </div>
                    </div>

                    <div class="mb-3 row">
                        <label class="col-lg-2 col-md-3 col-sm-4 col-form-label">Employee Status Id</label>
                        <div class="col-sm-8 col-md-9 col-lg-10">
                            <select name="employmentStatusId" class="select2 form-control" id="employmentStatus">
                                <option label="&nbsp;"></option>
                                <option value="24">Permanent</option>
                                <option value="25">Contractual</option>
                            </select>
                        </div>
                    </div>

                    <div class="mb-3 row">
                        <label class="col-lg-2 col-md-3 col-sm-4 col-form-label">Join Date</label>
                        <div class="col-sm-8 col-md-9 col-lg-10">
                            <input type="text" name="joinDate" class="form-control date-picker-close" id="joindate" value="{{ $date_now }}">
                        </div>
                    </div>

                    <div class="mb-3 row">
                        <label class="col-lg-2 col-md-3 col-sm-4 col-form-label">Position</label>
                        <div class="col-sm-8 col-md-9 col-lg-10">
                            <select name="posId" class="form-control" id="positionId" require>
                                <option label="&nbsp;"></option>
                                @foreach($plantilla_items as $item)
                                    <option value="{{ $item->id }}">{{ $item->uniq_item_no }}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>

                    <div class="mb-3 row">
                        <label class="col-lg-2 col-md-3 col-sm-4 col-form-label">Step</label>
                        <div class="col-sm-8 col-md-9 col-lg-10">
                            <select name="stepId" class="form-control" id="stepId" require>
                                <option label="&nbsp;"></option>
                                @foreach($steps as $step)
                                    <option value="{{ $step->id }}">{{ $step->description }}</option>
                                @endforeach
                            </select>
                        </div>
                    </div>

                    <div class="mb-3 row">
                        <label class="col-lg-2 col-md-3 col-sm-4 col-form-label">Is Current?</label>
                        <div class="col-sm-8 col-md-9 col-lg-10">
                            <div class="form-check form-check-inline">
                                <input class="form-check-input" type="radio" name="isPersonnelCurrent" id="inlineRadio1" value="1" />
                                <label class="form-check-label" for="inlineRadio1">Yes</label>
                            </div>
                            <div class="form-check form-check-inline">
                                <input class="form-check-input" type="radio" name="isPersonnelCurrent" id="inlineRadio2" value="0" />
                                <label class="form-check-label" for="inlineRadio2">No</label>
                            </div>
                        </div>
                    </div>

                    <div class="mb-3 row mt-5">
                        <div class="col-sm-8 col-md-9 col-lg-10 ms-auto">
                        <button type="submit" class="btn btn-outline-primary">Save</button>
                        </div>
                    </div>
                </form>

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

创建表personnel_details

CREATE TABLE `personnel_details` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `personnel_registry_id` bigint(20) unsigned NOT NULL,
  `service_id` smallint(5) unsigned NOT NULL,
  `org_code_id` smallint(5) unsigned DEFAULT NULL,
  `position_id` smallint(5) unsigned DEFAULT NULL,
  `emp_status_id` smallint(5) unsigned NOT NULL,
  `joining_date` date NOT NULL,
  `leave_date` date DEFAULT NULL,
  `ref_pos_id` bigint(20) unsigned NOT NULL,
  `step_id` smallint(5) unsigned NOT NULL,
  `is_current` tinyint(3) unsigned NOT NULL,
  `current_salary` decimal(10,2) DEFAULT NULL,
  `created_by` bigint(20) unsigned DEFAULT NULL,
  `updated_by` bigint(20) unsigned DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `FKPersonnelRegistryTOPersonnelDetails_idx` (`personnel_registry_id`),
  CONSTRAINT `FKPersonnelRegistryTOPersonnelDDetails` FOREIGN KEY (`personnel_registry_id`) REFERENCES `personnel_registry` (id)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci'

创建表personnel_registry

CREATE TABLE `personnel_registry` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `agency_emp_id` bigint(20) unsigned NOT NULL,
  `last_name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  `first_name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  `middle_name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  `extension_name` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
  
 ...

  `created_by` bigint(20) unsigned NOT NULL,
  `updated_by` bigint(20) unsigned NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
qni6mghb

qni6mghb1#

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

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

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

protected $guarded = [];

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

vlju58qv

vlju58qv2#

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

public function store(Request $request) {
    $personnelRegistry    = null;
    $personnerlRegistryId = (int)$request->input('personnelRegistryId');

    if($personnerlRegistryId && $request->isMethod('post')){
        $personnelRegistry = PersonnelRegistry::find($personnerlRegistryId)->getAttributes();
        $joinDate = $request->get('joinDate');

            $personnel                        = new PersonnelDetail();
            $personnel->personnel_registry_id = $personnerlRegistryId;
            $personnel->service_id            = $request->get('serviceId');
            $personnel->emp_status_id         = (int)$request->get('employmentStatusId');
            $personnel->joining_date          = date("Y-m-d", strtotime($joinDate));
            $personnel->ref_pos_id            = (int)$request->get('posId');
            $personnel->step_id               = (int)$request->get('stepId');
            $personnel->current_salary        = 1;
            $personnel->is_current            = (int)$request->get('isPersonnelCurrent');
            $personnel->created_by            = 1;
            $personnel->updated_by            = 1;

            $personnel->save();

        $request->session()->flash('status', 'Personnel details was created.');

        return redirect()->route('employee.show', ['employee' => $personnerlRegistryId]);
    } else {
       //Validation failed. Redirect
    }
}

不同的是

$personnel = new PersonnelDetail;

我把它改成:

$personnel = new PersonnelDetail();

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

相关问题