Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

j_watson's avatar

Live search doesn't work but also no errors.

I can't find the error with this one. No error appears in my laravel.log so I tried tracing it but to no avail. I'm for any suggestions.

js

    $('body').on('keyup','#search-patients',function()
    {
        var keyword = $(this).val();

        $.ajax({
            type: "POST",
            url: "{{ route('searchPatients')}}",
            dataType: "json",
            data: {keyword: keyword,
                    _token: '{{csrf_token()}}'
            },
            success: function(res){
                    $('#dynamic-row').empty();
                    $('#dynamic-row').append(res);
                }
        });
    });

controller

    public function searchPatients(Request $request)
    {
            $keyword = $request->get('keyword');

            $patient_admissions = DB::table('patients')
            ->join('admissions', 'patients.id', '=', 'admissions.patient_id')
            ->select('patients.id','patients.last_name as lname', 'patients.first_name as fname', 'patients.middle_name as mname', 'patients.suffix_name as sname', 'patients.status as status', 'admissions.admission_date_time as admission', 'admissions.discharge_date_time as discharge')
            ->where(function ($query) use ($keyword) {
                                $query->where('patients.id', 'LIKE', '%'.$keyword.'%')
                                        ->orWhere('patients.first_name', 'LIKE', '%'.$keyword.'%')
                                        ->orWhere('patients.middle_name', 'LIKE', '%'.$keyword.'%')
                                        ->orWhere('patients.last_name', 'LIKE', '%'.$keyword.'%')
                                        ->orWhere('patients.suffix_name', 'LIKE', '%'.$keyword.'%')
                                        ->orWhere('patients.address', 'LIKE', '%'.$keyword.'%')
                                        ->orWhere('patients.birthdate', 'LIKE', '%'.$keyword.'%')
                                        ->orWhere('patients.status', 'LIKE', '%'.$keyword.'%');
                            })
                            ->get();
    
            return view('includes.partials.patientSearch', compact('patient_admissions'))->render();
    }

partial blade

@foreach ($patient_admissions as $patient_admission)
<tr>
    <td><b><?php if(empty($patient_admission->mname) && isset($patient_admission->sname)  ) {
                        echo strtoupper($patient_admission->lname.', '.$patient_admission->fname.', '.$patient_admission->sname);
                    } else if (empty($patient_admission->sname) && isset($patient_admission->mname)){
                        echo strtoupper($patient_admission->lname.', '.$patient_admission->fname.', '.$patient_admission->mname);
                    } else if (empty($patient_admission->sname) && empty($patient_admission->mname)){
                        echo strtoupper($patient_admission->lname.', '.$patient_admission->fname);
                    }else {
                        echo strtoupper($patient_admission->lname.', '.$patient_admission->fname.', '.$patient_admission->mname.', '.$patient_admission->sname);
                    } ?></b></td>
    <td><b>{{ strtoupper($patient_admission->status); }}</b></td>
    <td><b><?php $date = date_create($patient_admission->admission); echo date_format($date, "h:i A - M. d") ?></b></td>
    <td><b><?php if(is_null($patient_admission->discharge)){ echo 'N/A';}else{$date = date_create($patient_admission->discharge); echo date_format($date, "h:i A");} ?></b></td>
    <td><a class="btn btn-outline-warning btn-sm" id="buttonEdit" href="{{ route('patients.show',  $patient_admission->id)}}"><b>VIEW</b></a>&nbsp<a class="btn btn-outline-info btn-sm" id="buttonEdit" href="{{ route('patients.edit',  $patient_admission->id)}}"><b>EDIT</b></a>&nbsp<a class="btn btn-outline-danger btn-sm" id="buttonEdit" href="{{ route('patientSoftDelete', $patient_admission->id) }}"><b>DELETE</b></a></td>
</tr>
@
0 likes
13 replies
Snapey's avatar

look in network tools in your browser

Is the post to the correct endpoint?

Is the search term being passed along?

What is the response?

1 like
j_watson's avatar

@Snapey I checked it out and the response and preview are correct when searching based on my database but the the table is still not changing based on the results.

Snapey's avatar

@j_watson So your issue is within your javascript, and what you do here $('#dynamic-row').append(res);

j_watson's avatar

@Snapey It seemed so but after a few debugging I noticed that after rendering the partial blade the results are not being returned. It just abruptly ends after rendering the new table rows.

1 like
Snapey's avatar

@j_watson In your controller, I don't think you need ->render()

Why are you using POST? This should really be a GET request if you are hoping to get a response.

Your blade is hard to read. Why did you drop into php for most of it and then use echo and concatenation = totally unnecessary.

I'm assuming your partial ends with @endforeach (its not shown) and nothing else?

j_watson's avatar

@Snapey I am using POST since I have to pass the user input unlike GET where in I can only request data.

j_watson's avatar

@amitsolanki24_

@amitsolanki24_

that's the response when searching "JAMES"

    <td><b>JOHNSON, JAMES</b></td>
    <td><b>ADMITTED</b></td>
    <td><b>09:20 PM - Apr. 01</b></td>
    <td><b>N/A</b></td>
    <td><a class="btn btn-outline-warning btn-sm" id="buttonEdit" href="http://127.0.0.1:8000/patients/43"><b>VIEW</b></a>&nbsp<a class="btn btn-outline-info btn-sm" id="buttonEdit" href="http://127.0.0.1:8000/patients/43/edit"><b>EDIT</b></a>&nbsp<a class="btn btn-outline-danger btn-sm" id="buttonEdit" href="http://127.0.0.1:8000/patients/43/soft-delete"><b>DELETE</b></a></td>
</tr>
j_watson's avatar
j_watson
OP
Best Answer
Level 2

@Snapey so this is what I did and it works just fine but I don't know if it's the best way to do it

            $results = view('includes.partials.patientSearch', compact('patient_admissions'));
            $view = $results->render();

            return json_encode($view);

I think after rendering the results to the partial blade, the partial blade has no way of finding it's way back to the ajax to append the results

gych's avatar

@j_watson In your request you are using dataType json, which expects a json response. Set dataType to html and try again to directly return the rendered html instead of using json_encode.

1 like

Please or to participate in this conversation.