Home > Net >  Laravel autocomplete search with sql stored procedure not working
Laravel autocomplete search with sql stored procedure not working

Time:12-23

I have a search field with id_registrasi with some values. I don't know if its a problem but i work with sql stored procedure and laravel query builder to call table in the database. I'm trying to return it and its works fine, but not when i work with view blade. This is my code so far:

Routes

Route::get('/rj/cari/index', [RJController::class, 'indexCari']);
Route::post('/rj/cari', [RJController::class, 'cari'])->name('cari');

Controller

    public function indexCari()
{
    return view('rjs.rj-input');
}

public function cari(Request $request)
{
    $search = $request->search;

    if ($search == '') {
        $results = DB::select("EXEC sp_kasir_daftar_harian_pasien_detail 
        @tgl='2021-11-21',
        @id_jenis_transaksi=1,
        @kd_cara_bayar=1
        ");
    }

    $response = array();
    foreach ($results as $r) {
        $response[] = array(
            "label" => $r->id_registrasi, "value" => $r->id_pasien, "value1" => $r->nm_pasien, "value2" => $r->alamat,
            "value3" => $r->nm_cara_bayar, "value4" => $r->nm_jaminan, "value5" => $r->total_biaya,
            "value6" => $r->nm_status_bayar
        );
    }

View Blade:

   <script type="text/javascript">

var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');

$(document).ready(function(){

 $( "#id_registrasi" ).autocomplete({
    source: function( request, response ) {
       // Fetch data
       $.ajax({
         url:"{{route('cari')}}",
         type: 'post',
         dataType: "json",
         data: {
            _token: CSRF_TOKEN,
            search: request.term
         },
         success: function( data ) {
            response( data );
         }
       });
    },
    select: function (event, ui) {
      // Set selection
      $('#id_registrasi').val(ui.item.label); // display the selected text
      $('#id_pasien').val(ui.item.value); 
      $('#nm_pasien').val(ui.item.value1);
      $('#alamat').val(ui.item.value2);
      $('#nm_cara_bayar').val(ui.item.value3);
      $('#nm_jaminan').val(ui.item.value4);
      $('#total_biaya').val(ui.item.value5);
      $('#nm_status_bayar').val(ui.item.value6);
      return false;
    }
 });
});

When return $response; it works fine. I get the result. But when return response()->json($response); i get jquery.min.js:2 POST http://127.0.0.1:8000/rj/cari 500 (Internal Server Error) when i try to fill the input form for search. Please help?

CodePudding user response:

try using

error: function(response){
 console.log(response)
}

to see what is the cause of 500 error

CodePudding user response:

    public function cari(Request $request)
{

    $search = $request->search;

    if ($search == '') {
        $results = DB::raw("SET NOCOUNT ON;EXEC sp_kasir_daftar_harian_pasien_detail 
        @tgl='2021-11-21',
        @id_jenis_transaksi=1,
        @kd_cara_bayar=1
        ");
    }

    $response = array();
    foreach ($results as $r) {
        $response[] = array(
            "label" => $r->id_registrasi, "value" => $r->id_pasien, "value1" => $r->nm_pasien, "value2" => $r->alamat,
            "value3" => $r->nm_cara_bayar, "value4" => $r->nm_jaminan, "value5" => $r->total_biaya,
            "value6" => $r->nm_status_bayar
        );
    }

    return response('success', 200);

@Atoli Gaming added return response('success', 200) and its still not working, either with response(json_encode($response),200)

  • Related