0

I am new to laravel. I want to update a database row. For this purpose, i want to pass the id of the corresponding row to the controller from javascript. I can get the id in javascript file from view. But i get an error ( ErrorException in PatientController.php line 80: Creating default object from empty value) while doing this. Because controller does not get that id. How can i pass the id and resolve this problem?

my view (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>

my 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
        }
    });
});

my route:

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

my controller():

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]);
    }
Nazem Mahmud Piash
  • 1,053
  • 1
  • 13
  • 34
  • 1
    Laravel using use Illuminate\Http\Request; to handle requests not exactly sure about HttpRequest I assume it is guzzle. try to import laravel request class and try to dd($request->all()'); lets figure out are you getting data first or not – Anar Bayramov Mar 23 '17 at 08:29
  • yeah, i know, i just use Illuminate\Http\Request as HttpRequest; that's all. and i use dd($request->all()); i get data perfectly. Now, what can i do? @Rodrane – Nazem Mahmud Piash Mar 23 '17 at 08:57
  • so do you recieve data ? – Anar Bayramov Mar 23 '17 at 09:00
  • yes i receive data, i just dont receive the corresponding id from javascript. When i use dd($patient);, it shows me null.. @Rodrane – Nazem Mahmud Piash Mar 23 '17 at 09:02
  • where exactly you get patientId ? why dont you send it as a hidden input in your form seems like you dont post everything with ajax request? – Anar Bayramov Mar 23 '17 at 09:05
  • Thanx, your hidden input idea works. But how can i pass the id with the help of script? I get it from my view "
    " and store it in script " patientId = event.target.parentNode.parentNode.dataset['patientid']; " @Rodrane
    – Nazem Mahmud Piash Mar 23 '17 at 09:15
  • if hidden input works you can just append data as hidden input. check this http://stackoverflow.com/questions/2408043/jquery-create-hidden-form-element-on-the-fly – Anar Bayramov Mar 23 '17 at 09:19

0 Answers0