laravel 5.4:如何将javascript值传递给控制器​​以更新数据

我是laravel新手。 我想更新一个数据库行。 为此,我想将相应行的id从javascript传递给控制器​​。 我可以从视图中获取JavaScript文件中的id。 但是,在执行此操作时,出现错误(PatientController.php中的ErrorException第80行:从空值创建默认对象)。 因为控制器没有得到该ID。 我如何通过身份证并解决此问题?

我的看法(editPatientView.blade.php):

<div class="patient-form-view" data-patientid="{{ $patient->id }}">
        <h2>Patient Details</h2>
        <form action="{{ route('savepatientinfo') }}" method="post"> {{-- route('postCreate') --}}
            <div class="form-group">
                <label for="name">Name *</label>
                <input class="form-control" id="patient_name" name="patient_name" type="text" value="" id="example-text-input">
            </div>

            <div class="col-md-6" style="float: left; width: 100%; padding-left: 0px">
                <div class="form-group" style="float: left; width: 48%; ">
                    <label for="age">Age *</label>
                    <input class="form-control" id="patient_age" name="patient_age" type="text" value="">
                </div>
                <div class="form-group" style="float: left; width: 48%; margin-left: 22.5px;">
                    <label for="weight">Weight</label>
                    <input class="form-control" id="patient_weight" name="patient_weight" type="text" value="">
                </div>
            </div>

            <div class="form-group">
              <label for="symptom">Symptoms *</label>
              <textarea class="form-control" name="symptom" id="exampleTextarea" rows="3" value="{{ $patient->symptom }}"></textarea>
            </div>
            <div class="form-group">
              <label for="test">Tests (if any)</label>
              <textarea class="form-control" name="test" id="exampleTextarea" rows="3" value="{{ $patient->test }}"></textarea>
            </div>

            <div class="form-group">
              <label for="Medicine">Medicine</label>
              <textarea class="form-control" name="medicine" id="exampleTextarea" rows="3"></textarea>
            </div>
            <div class="form-group">
                <label for="return-date" class="col-2 col-form-label">Return Date</label>
                <input class="form-control" name="return-date" type="date" value="{{ $patient->return_date }}" id="example-date-input">
            </div>
            <button type="submit" class="btn btn-primary" id="edit">Save</button>
            <input type="hidden" name="_token" value="{{ Session::token() }}">
        </form>
    </div>
    <script>
        var urlEdit = '{{ route('savepatientinfo') }}';
        var token = '{{ Session:: token() }}';
    </script>

我的脚本(script.js):

var patientId = 0;
$('#edit').on('click', function (event) {
    patientId = event.target.parentNode.parentNode.dataset['patientid'];
    alert(patientId);
    $.ajax({
        method: 'POST',
        url : urlEdit,
        data: { 
            patientId: patientId,
            _token: token
        }
    });
});

我的路线:

Route::post('/savepatientinfo', [
        'uses'=> 'PatientController@SavePatientInfo',
        'as'=>'savepatientinfo'
    ]);

我的控制器():

public function SavePatientInfo(HttpRequest $request)
    {
        //        validation...
        $this->validate($request, [
            'patient_name'=> 'required|max:2000',
            'patient_age'=> 'required',
            'symptom'=> 'required',
        ]);
        $patient = Patient::find($request['patientId']) ;

//        dd($patient);
        $user = Auth::user();

        $patient->name = $request['patient_name'];
        $patient->age = $request['patient_age'];
        $patient->weight = $request['patient_weight'];
        $patient->symptom = $request['symptom'];
        $patient->test = $request['test'];
        $patient->return_date = $request['return-date'];

        $patient->user()->associate($user);

        $patient->update();   

        if($request->user()->patients()->save($patient))
        {
            $message = 'Patient information enrolled successfully' ;
        }
        return redirect()->route('showpatientinfo', ['user_id'=> $patient->user->id, 'patient_id'=>$patient->id])->with(['message'=>$message]);
    }
链接地址: http://www.djcxy.com/p/83491.html

上一篇: laravel 5.4: how to pass value from javascript to controller for updating data

下一篇: Using href in jquery with get method