首页 > 解决方案 > 如何获取每个特定 ID 的详细信息?

问题描述

在我的列表视图中,我有休假的所有详细信息。但是当我单击详细信息时,它会显示一个弹出窗口。在弹出框中,它必须获取并给我特定字段的所有详细信息,但它总是给我最后插入记录的详细信息

这是我的列表文件的代码

 <table id="myTable" class="table table-bordered table-striped">
  <thead>
    <tr>
      <th>Employee Name</th>
      <th>Leave Type</th>
      <th>Leave Duration</th>
      <th>Applied On</th>
      <th>Status</th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody>
    @if($allLeaves != null)
    @foreach($allLeaves as $leave)
    <tr>
      <td> {{ $leave->username }} </td>
      <td> {{ $leave->typeOfLeave }} </td>
      <td>
        {{ $leave->startDate }} To
        {{ $leave->endDate }}
      </td>
      <td> {{ $leave->startDate }} </td>
      <td>
        @if($leave['status']=='Pending')
        <span class="btn btn-warning">Pending</span>
        @elseif($leave['status']=='Approved')
        <span class="btn btn-success">Approved</span>
        @else
        <span class="btn btn-danger">Rejected</span>
        @endif
      </td>
      <td><a href = "{{ route( 'handle_leave', $leave->id)}}" data-toggle="modal" data-target="#details" class="btn btn-info text-white">Details</a></td>
    </tr>                                    
  </tbody>
</table>

在同一页面中,我编写了获取记录的代码

 <form id="myform" class="form-horizontal" method="POST" action="{{ route('approve_leave', $leave->id) }}">
  @csrf
  <div class="card-body">
    <div class="row">
      <label class="col-md-6"><strong> Employee Name</strong></label>
      <div class="col-md-6">
        <input type="text" class="form-control" id="emp_name" disabled value="{{$leave->username}}" style="border:none">
      </div>
    </div>
    <br>
    <div class="row">
      <label class="col-md-6"><strong>Leave Type</strong></label>
      <div class="col-md-6">
        <input type="text" class="form-control" id="leavetype" disabled value="{{$leave->typeOfLeave}}" style="border:none">
      </div>
    </div>
    <br>
    <div class="row">
      <label class="col-md-6"><strong>Leave Duration</strong></label>
      <div class="col-md-6">
        <input type="text" class="form-control" id="leaveduration" disabled value="{{ $leave->startDate }} To {{ $leave->endDate }}" style="border:none">
      </div>
    </div>
    <br>
    <div class="row">
      <label class="col-md-6"><strong>Reason</strong></label>
      <div class="col-md-6">
        <input type="text" class="form-control" id="reason" disabled value="{{$leave->reasonForLeave}}" style="border:none">
      </div>
    </div>
    <br>
    <div class="row">

      <label class="col-md-6"><strong>Applied on</strong></label>
      <div class="col-md-6">
        <input type="text" class="form-control" id="appliedon" disabled value="{{$leave->startDate}}" style="border:none">
      </div>
    </div>
    <br>
    <div class="row">
      <label class="col-md-6"><strong>Action</strong></label>
      <div class="col-md-6">
        <select class="form-control" id="status" name="status" value="{{$leave->status}}">
          <option value="Pending" selected="selected">Pending</option>
          <option value="Approved">Approved</option>
          <option value="Rejected">Rejected</option>
        </select>
      </div>
    </div>
    @endforeach
    <br>
    <div class="row">
      <label class="col-md-6"><strong>Reason For Action</strong></label>
      <div class="col-md-6">
        <input type="text" class="form-control" id="reason" name="reasonForAction" placeholder="Reason Of Action" style="border:none">
      </div>
    </div>
    <br>
    <div class="modal-footer">
      <button type="submit" class="btn btn-info waves-effect" data-dismiss="modal">Save</button>
      <button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Cancel</button>
    </div>

  </div>
</form>

这是我在控制器文件中编写的代码

//Code Of list view

    public function listOfLeave()
    {
        $allLeaves = LeaveManagement::all();
        return view('pages.leavelist', compact('allLeaves'));
    }

//Code of click on details button and fetch record of that particular id

    public function handleLeave($id)
    {
        $leave = LeaveManagement::find($id);
        return view('pages.leavelist', compact('leave', 'id'));
    }

//code of approve reject leave and change the status of leave
    public function approveLeave(Request $request ,$id)
    {
        $leave = LeaveManagement::find($id);
        $leave->status = $request->get('status');
        $leave->reasonForAction = $request->get('reasonForAction');
        $leave->save();

        return view('pages.leavelist');
    }

标签: laravelmongodbeloquentcontroller

解决方案


如果您想根据用户的 id 显示数据,那么您必须传递每个字段data-fieldname,然后您可以获取该字段的数据,如脚本中所示。

按钮:

<button type="button" 
       class="btn btn-xs btn-default confirm-modal" 
       data-toggle="modal" 
       data-target="#model" 
       data-id="{{ $singleRecord->id }}" 
       data-name="{{ $singleRecord->full_name  }}"
       data-leave_reason="{{ $singleRecord->leave_reason }}"                                                                                   
       data-from_date="{{ date('d-F-Y', strtotime($singleRecord->from_date)) }}"
       data-to_date="{{ date('d-F-Y', strtotime($singleRecord->to_date)) }}"                                                                         
 >Details </button>

并添加脚本

 <script type="text/javascript">
        $('.confirm-modal').click(function (event) {
            $('.employee_name').text($(this).data('name'));
            $('#from_date').text($(this).data('from_date'));
            $('#to_date').text($(this).data('to_date'));
            $('#total_leaves').text($(this).data('total_leaves'));
            $('.leave_reason').text($(this).data('leave_reason'));
        })
    </script>

在模型 div 中,添加每个 id 以显示该数据。

<div class="col-md-12">
      <label for="to_date">To Date:</label>
      <span id="to_date"></span>

希望这可以帮助 :)


推荐阅读