首页 > 解决方案 > 如何通过 Ajax 检索记录并在 Laravel 表中显示

问题描述

I want show the specific record by id through on click event in model 

该模型正在纸上,但没有通过 url 函数获取任何详细信息

     <button id="{{$res->id}}" class="btn btn-info" data-toggle="modal" data-target="#modal-xl" onclick="viewslip(this.id)">View Slip</button>

我没有得到任何错误,所以我可以调试它。
这里我使用 ajx 请求通过 id 函数 viewlip(id){
$.ajax({

   url: "{{url('/view/payment/slip')}}"/+id,
   type: "GET",
   dataType:"json",
   success:function(data){
    $('#fname').text(data.paymentslip.firstname); 
    $('#lname').text(data.paymentslip.lastname);        
          
   }

})
}

model where I am appending data

    <div class="table-responsive">
                    <table class="table">
                      <tr>
                        <th style="width:50%">First Name:</th>
                        <td><h1 id="fname"></h1></td>
                      </tr>
                      <tr>
                        <th style="width:50%">last Name:</th>
                        <td><h1 id="lname"></h1></td>
                      </tr>
                      <tr>


        Method in Controller

         public function viewslip($id){         
         $paymentslip= Order::where('reservationid', '=', $id)->firstOrFail();   
         return response::json(array(
         
         'paymentslip'=>$paymentslip,
         
         ));        
            
        }     


 

标签: phpjqueryajaxlaravel

解决方案


使用以下代码

onclick="viewslip({{$res->id}})"

在你的函数中使用

function viewslip($orderId){

    var orderId = $orderId;
    $.ajax({

       url:"{{url('')}}/view/payment/slip/"+orderId,  // Your problem was here
       type: "GET",
       dataType:"json",
       success:function(data){
           $('#fname').text(data.paymentslip.firstname); 
           $('#lname').text(data.paymentslip.lastname);        
          
      }
})

推荐阅读