Bagadeeshkumar
Bagadeeshkumar

Reputation: 1

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

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 App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\patient;
use App\Models\prescription_management;
use Illuminate\Support\Facades\DB;

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

}  

Upvotes: 0

Views: 350

Answers (1)

Mihai Matei
Mihai Matei

Reputation: 24276

You can't do that. Also, there is no reason to try doing so in an application, unless you have multiple authentication types, being the case when you usually group all the routes by prefixes.

What you must do is to use different requests methods for each type of action:

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

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

Just make sure that you use the right method when doing the ajax request: GET, POST, PUT, etc

Upvotes: 1

Related Questions