【问题标题】:How to make multiple ajax routes for same url(patient_service) and same controller(Billing.php) with different functions in that controller如何为相同的 url(patient_service) 和相同的控制器(Billing.php) 在该控制器中使用不同的功能创建多个 ajax 路由
【发布时间】:2021-04-21 08:16:49
【问题描述】:

这里我没有任何错误,但它不起作用

我不能为同一个 URL 和同一个控制器使用两条路由,但在该控制器中具有不同的功能

希望我能得到答案

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']);

我能否为相同的 URL 和具有不同功能的相同控制器发出 ajax 发布请求,但它不起作用我只能使用一个发布请求

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

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

Billing.php(控制器)

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

}  

【问题讨论】:

    标签: php laravel-8


    【解决方案1】:

    你不能那样做。此外,没有理由在应用程序中尝试这样做,除非您有多种身份验证类型,例如您通常按前缀对所有路由进行分组。

    您必须做的是对每种类型的操作使用不同的请求方法:

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

    请确保在执行 ajax 请求时使用正确的方法:GETPOSTPUT

    【讨论】:

      猜你喜欢
      • 2017-04-05
      • 2015-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-24
      • 2016-03-29
      • 1970-01-01
      • 2020-02-27
      相关资源
      最近更新 更多