将jquery步骤(通过validate)中的数据保存到3个不同的数据库中

时间:2019-06-18 12:54:40

标签: php laravel forms

我已经使用jquery步骤创建了一个表单,并且我希望用户将数据填充到Practitioner,PractitionerSpecialty,PractitionerCompetenceLevel三个不同的数据库中。完成表单后,我使用form.submit();创建请求。但是在网站上返回419错误,我不确定是否正确创建了控制器功能,这可能是原因。

blade.php中的表单:

<form method="post" id="practitioner-form" action="/practitioner">
        <h3>Practitioner</h3>
        <section>
            <legend>Practitioner Information</legend>
            <div class="j-row">
                <div class="j-unit" style="width: 45%; display: inline-block">
                    <label for="effective_date" class="j-label">{{trans('personalData.effectiveDate')}}</label>

                    <div class="input-group">
                        <input name="main_effective_date_create" id="main_effective_date_create"
                               type="date" class="form-control required">
                    </div>
                </div>
                <div class="j-unit" style="margin-left: 9%; width: 45%; display: inline-block">
                    <label for="expiry_date" class="j-label">{{trans('personalData.expiryDate')}}</label>

                    <div class="input-group">
                        <input name="main_expiry_date_create" id="main_expiry_date_create"
                               type="date" class="form-control">
                    </div>
                </div>
            </div>

            <div class="j-row">
                <div class="j-unit" style="width: 45%; display: inline-block">
                    <label for="phone" class="j-label">{{trans('personalData.phoneNumber')}}</label>

                    <div class="input-group">
                        <input name="main_phone_create" id="main_phone_create"
                               type="tel" class="form-control">
                    </div>
                </div>
                <div class="j-unit" style="margin-left: 9%; width: 45%; display: inline-block">
                    <label for="mobile" class="j-label">{{trans('personalData.mobileNumber')}}</label>

                    <div class="input-group">
                        <input name="main_mobile_create" id="main_mobil_create"
                               type="tel" class="form-control">
                    </div>
                </div>
            </div>
            <div class="j-row">
                <div class="j-unit">
                    <label for="email" class="j-label">{{trans('personalData.email')}}</label>

                    <div class="input-group">
                        <input name="main_email_create" id="main_email_create"
                               type="tel" class="form-control">
                    </div>
                </div>
            </div>
        </section>
        <h3>{{trans('settings.specialty')}}</h3>
        <section>
            <legend>{{trans('personalData.practitionersSpecialty')}}</legend>
            <div class="j-row">
                <label for="practitioner_specialty_id"
                       class="block">Select {{trans('settings.specialty')}}</label>
                <select name="practitioner_specialty_id_create"
                        id="practitioner_specialty_id_create"
                        class="form-control">
                    @foreach($specialties as $specialty)
                        <option
                            value="{{$specialty->practitioner_specialty_id}}">{{$specialty->name}}</option>
                    @endforeach
                </select>
            </div>
            <div class="j-row">
                <div class="j-unit" style="width: 45%; display: inline-block">
                    <label for="effective_date_specialty"
                           class="j-label">{{trans('personalData.effectiveDate')}}</label>

                    <div class="input-group">
                        <input name="specialty_effective_date_create"
                               id="specialty_effective_date_create"
                               type="date" class="form-control required">
                    </div>
                </div>
                <div class="j-unit" style="margin-left: 9%; width: 45%; display: inline-block">
                    <label for="expiry_date_specialty" class="j-label">{{trans('personalData.expiryDate')}}</label>

                    <div class="input-group">
                        <input name="specialty_expiry_date_create" id="specialty_expiry_date_create"
                               type="date" class="form-control">
                    </div>
                </div>
            </div>
        </section>
        <h3>{{trans('settings.competenceLevel')}}</h3>
        <section>
            <legend>{{trans('personalData.practitionersCompetenceLevel')}}</legend>
            <div class="j-row">
                <label for="competence_level" class="block">{{trans('settings.competenceLevel')}}</label>
                <select name="practitioner_competence_level_id_create"
                        id="practitioner_competence_level_id_create"
                        class="form-control">
                    @foreach($competence_levels as $level)
                        <option
                            value="{{$level->competence_level_id}}">{{$level->name}}</option>
                    @endforeach
                </select>
            </div>
            <div class="j-row">
                <div class="j-unit" style="width: 45%; display: inline-block">
                    <label for="effective_date_competence"
                           class="j-label">{{trans('personalData.effectiveDate')}}</label>

                    <div class="input-group">
                        <input name="competence_effective_date_create"
                               id="competence_effective_date_create"
                               type="date" class="form-control required">
                    </div>
                </div>
                <div class="j-unit" style="margin-left: 9%; width: 45%; display: inline-block">
                    <label for="expiry_date_competence" class="j-label">{{trans('personalData.expiryDate')}}</label>

                    <div class="input-group">
                        <input name="competence_expiry_date_create" id="competence_expiry_date_create"
                               type="date" class="form-control">
                    </div>
                </div>
            </div>
            <div class="form-group row">
                <div class="col-lg-12">
                    <label for="notes" class="block">{{trans('tables.notes')}}</label>
                </div>
                <div class="col-lg-12">
                    <textarea placeholder="Description" cols="80" rows="5" name="notes_create"></textarea>
                </div>
            </div>
        </section>
    </form>

将数据添加到数据库的途径:

Route::post('/practitioner', 'CRUD\Settings\PractitionerController@storePractitioner')->name('addPractitioner');

具有存储功能的控制器:

public function storePractitioner(Request $request, $id){
        $practitioner_id = $this->practitionerRepository->getIdByPersonId($id);
        $practitioner = Practitioner::where('person_id', $id);
        $practitioner_specialty = PractitionerSpecialty::where('practitioner_id', $practitioner_id);
        $practitioner_competence_level = PractitionerCompetenceLevel::where('practitioner_id', $practitioner_id);

        $practitioner->effective_date=$request->get('main_effective_date_create');
        $practitioner->expiry_date=$request->get('main_expiry_date_create');
        $practitioner->phone=$request->get('main_phone_create');
        $practitioner->mobile=$request->get('main_mobile_create');
        $practitioner->email=$request->get('main_email_create');
        $practitioner_specialty->practitioner_specialty_id=$request->get('practitioner_specialty_id_create');
        $practitioner_specialty->effective_date=$request->get('specialty_effective_date_create');
        $practitioner_specialty->expiry_date=$request->get('specialty_expiry_date_create');
        $practitioner_competence_level->practitioner_competence_level_id=$request->get('practitioner_competence_level_id_create');
        $practitioner_competence_level->effective_date=$request->get('competence_effective_date_create');
        $practitioner_competence_level->expiry_date=$request->get('competence_expiry_date_create');
        $practitioner_competence_level->notes=$request->get('notes_create');
        return back()->with('success', 'New practitioner has been added');
    }

1 个答案:

答案 0 :(得分:1)

您的表单中缺少某些内容。在表单开始标记后添加{{ csrf_field() }}到表单