【发布时间】: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);
}
});
}
【问题讨论】: