Why ajax call is not fetching data from Database (Laravel Project)

  ajax, javascript, laravel, php

So the thing is when I click on the button, it will call an ajax function which will direct to controller file and store the data in FollowupTreatment variable, but in view file it isn’t printing data and just print N/A

So here is the button

<tr>
      <th scope="row">
         <a role="button"
          style="text-decoration:underline;"
             data-toggle="modal"
              data-target="#summaryModal" onClick="fetchSummary({{$eachPatient->IdPatient}})">
                  {{$eachPatient->IdPatient}}
                  </a>
          </th>

Here is the fetchsummary function

function fetchSummary(IdPatient){
            $.ajax({
                type:"POST",
                url:"{{route('fetchSummary')}}",
                data:{"_token": "{{ csrf_token() }}","IdPatient" :IdPatient},
                success:function(response){
                    console.log(response);
                    $('#summaryModalBody').html(response);
                },
                error:function(error){
                    console.log(error);
                }
            });
        }

Here is the route

    Route::post('/fetchSummary', [TreatmentController::class, 'fetchSummary'])->name('fetchSummary');

Here is the Treatmentcontroller class

public function fetchSummary(Request $request){
    
    $FollowupTreatment=DB::table("Treatment")
    ->join("Treatment_Followup","Treatment_Followup.treatmeant_id","Treatment.IdTreatment")
    ->join("Followup","Followup.IdFollowup","Treatment_Followup.follow_up")
    ->where('numerosettimana',4)
    ->where("Treatment.IdPatient",$request->IdPatient)
    
    ->get();

    return view('user_view.PatientTreatmentSummary',compact('FollowupTreatment'));
}

And Fianlly here is the View File (PatientTreatmentSummary)

<tr>
                         <!-- complicanze minori=minor complications -->
                        <th  class="table-info">Complicanze Minori</th>
                        @for($i=0;$i<6;$i++)
                        <td class="data-{{$i+1}}">
                            {{isset($FollowupTreatment[$i]->complicanzeminori)?$FollowupTreatment[$i]->complicanzeminori:'N/A'}}
                        </td>
                        @endfor
                    </tr>
                    <tr>
                         <!-- complicanze maggiori=major complications -->
                        <th  class="table-info">Complicanze Maggiori</th>
                        @for($i=0;$i<6;$i++)
                        <td class="data-{{$i+1}}">
                            {{isset($FollowupTreatment[$i]->complicanzemaggiori)?$FollowupTreatment[$i]->complicanzemaggiori:'N/A'}}
                        </td>
                        @endfor
                    </tr>

Here is the output of view file

enter image description here

It is showing this error in console

Uncaught ReferenceError: $ is not defined
at 1?_token=hJP8thspmb7DC9WTHnMWhWLtDHf74rLtrxivkWpZ&_method=get:25

Source: Laravel

Leave a Reply