How to make multiple ajax routes for same url(patient_service) and same controller(Billing.php) with different functions in that controller

  laravel-8, php

Here I haven’t got any error but it is not working

I cannot able to use two routes for the same URL and same controller with different functions in that controller

hope I will get answer

web.php

Route::get('/', function () {
  return view('admin');
});


Route::get('/manage_invoice', function () {
  return view('manage');
});

Route::get('/patient_service', function () {
  return view('service');
});

//this is where I got confused
==============================
Route::get('/patient_service', [Billing::class, 'showPatient']);

Can I able to make the ajax post request for same URL and same controller with different functions but it is not working I can able to use only one post request

Route::post('/patient_service', [Billing::class, 'showDetails']);

Route::post('/patient_service', [Billing::class, 'updateService']);

Billing.php(controller)

    <?php

namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppModelspatient;
use AppModelsprescription_management;
use IlluminateSupportFacadesDB;

class Billing extends Controller
{
 
    public function showPatient()
    {
        $data = patient::all();
        return view('service', ['patients' => $data]);
    }

    public function showDetails(Request $reqid)
    {
        $id = $reqid->id;
        $medicines = DB::table('prescription management')
            ->where('Patient ID', $id)
            ->get();
        $services = DB::table('patient service details')
            ->where('Patient_id', $id)
            ->get();
        $output = "";

        foreach ($services as $service) {
            $output .= '<tr>
            <td><i class="fa fa-trash"></i> ' . $service->Medical_Summary . '</td>
            <td>' . $service->Amount . '</td>';
        }

        foreach ($medicines as $medicine) {
            $output .= '
        <td><i class="fa fa-trash"></i> ' . $medicine->Name_of_the_medicine . '</td>
        <td><input type="text" name="amount" class="form-control" placeholder="Amount"></td>
        </tr>';
        }

        return response()->json($output);
    }

    public function updateService(Request $req)
    {
        $id = $req->id;
        $serup = DB::table('patient service details')
            ->where('Patient_id', $id)
            ->orderBy('ID', 'desc')
            ->get();
        return response()->json($serup);
    }
}

main.js

$(document).ready(function(){
 
    // Initialize select2
    $("#selUser").select2();
  
    // Read selected option
    $('#selUser').change(function(){
      // let username = $('#selUser option:selected').text();
      let userid = $('#selUser').val();
      loadTable(userid);
    });

});

function loadTable(userid){
  $.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

  $.ajax({
    
    url: "/patient_service",
    method: "POST",
    dataType: "json",
    data: {
        '_token': $('input[name=_token]').val(),
        id: userid
    },
    success: function(data){
      $("#service").html(data);      
    }
    
});
} 


function update(){
  let serviceId = $('#s_id').val();
  let serviceSummary = $('#medsum').val();
  let amount = $('#amount').val();
  let userid = $('#selUser').val();

  $.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

  $.ajax({
    
    url: "/update",
    method: "POST",
    dataType: "json",
    data: {
        '_token': $('input[name=_token]').val(),
        id: userid,
        serid: serviceId,
        summary: serviceSummary,
        amount: amount
    },
    success: function(data){
      // loadTable(userid);
      console.log(data);
    }
    
});

}  

Source: Ask PHP

LEAVE A COMMENT