我想根据在URl中传递的ID显示值

时间:2017-04-22 05:45:18

标签: mysql laravel-5.4

我想根据特定ID显示数据库值。我在URL中传递了该ID,然后重定向到另一个带ID的页面。在该页面中,我必须显示该特定用户的记录。

这是控制器功能:

public function consultants()
{
    return view('Hr/view-request-candidates', [
        'consultant' => HrRequestConsultant::all(),
    ]);
}

路线:

Route::get(
    '/view-request-candidates/{hr_request_id}',
    'Hr\HrDashboardController@consultants'
);

URL:

<a href="/view-request-candidates/{{$row->id}}" class="btn btn-sm btn-primary">CANDIDATES</a>

查看:

<div class="col-md-12">
  <table class="table table-striped">
    <tbody>
      <tr>
        <th>Hr Request ID</th>
        <th>Consultant ID</th>
        <th>No Of Candidates</th>
        <th>Actions</th>
      </tr>

  @foreach($consultant as $row)
      <tr>
        <td>{{$row->hr_request_id}}</td>
        <td>{{$row->consultant_id}}</td>
        <td>{{30}}</td>

        <td>
          <a href="/view-hr-candidates" class="btn btn-primary">View</button>
        </td>
      </tr>
  @endforeach

我该怎么做?

2 个答案:

答案 0 :(得分:1)

<强>路线::

Route::get('home','HrDashboardController@home');

Route::get('view-hr-candidates/{hr_request_id}','HrDashboardController@consultants');

<强>控制器::

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\HrRequestConsultant;
use DB;
class HrDashboardController extends Controller
{ 


   public function home(){  
   $home = HrRequestConsultant::select('id','hr_request_id','consultant_id')->get();
   return view('home')->with('home', $home); 

   //Its View:  home.blade.php file

 }


   public function consultants($id){ 

   $result = HrRequestConsultant::select('id','hr_request_id','consultant_id')->where('hr_request_id',$id)->first(); 

   return $returnview = view('Hr.view-request-candidates')->with('result', $result); 
     // Its view-request-candidates.blade.php file InSide the Hr Dir.  
   }

}

<强> home.blade.php ::

<body>
    <div class="flex-center position-ref full-height">
        <div class="content">
            <div class="title m-b-md">  View Details  </div>
                <div class="col-md-12">

                  <table border="1" style='color: black'>

                <tr>
                <th>Hr Request ID</th>
                <th>Consultant ID</th> 
                <th>No Of Candidates</th>
                <th>Actions</th>
               </tr>

           @foreach ($home as $data){
                <tr>
                <td> {{ $data['hr_request_id'] }} </td>
                <td> {{ $data['consultant_id'] }} </td>
                <td> 30 </td>
                <td> <a href="view-hr-candidates/{{ $data['hr_request_id'] }}" class="btn btn-primary">View</a> </td>
               </tr>
            @endforeach

               </table>
       </div>
    </div>
  </div>
</body>

查看请求-candidates.blade.php

<h1> Hello .. This Is Request Id:   {{ $result['hr_request_id'] }} </h1>
<br>
<h1> Hello .. This Is Consultant Id: {{ $result['consultant_id'] }} </h1>

答案 1 :(得分:0)

要获取单个记录,请使用find方法而不是all

public function consultants($id)
{
    return view('Hr/view-request-candidates', [
        'consultant' => HrRequestConsultant::find($id),
    ]);
}

最佳做法是在需要该路线的地方命名路线并使用名称。所以改变这样的路线:

Route::get(
'/view-request-candidates/{hr_request_id}',
'Hr\HrDashboardController@consultants')->name('view.consultant');

从视图中删除foreach循环并将路径更改为指定路径:

<div class="col-md-12">
    <table class="table table-striped">
      <tbody>
          <tr>
            <th>Hr Request ID</th>
            <th>Consultant ID</th>
            <th>No Of Candidates</th>
            <th>Actions</th>
          </tr>
            <tr>
                <td>{{$consultant->hr_request_id}}</td>
                <td>{{$consultant->consultant_id}}</td>
                <td>{{30}}</td>

                <td>
                   <a href="{{ route('view.consultant', ['hr_request_id' => $consultant->hr_request_id]) }}" class="btn btn-primary">View</button>
                </td>
            </tr>