2017-04-22 42 views
-1

ID傳遞我想顯示數據庫值取決於特定的ID。我在URL中傳遞該ID,然後重定向到另一個帶ID的頁面。在該頁面中,我必須顯示特定用戶的記錄。我想要顯示的值取決於通過URl

這裏是控制器功能:

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

路線:

Route::get(
    '/view-request-candidates/{hr_request_id}', 
    'Hr\[email protected]' 
); 

網址:

<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 

我該怎麼做?

+0

後的路線聲明你可以在公共職能顧問ID ($ Id),然後你可以直接根據id nad重定向獲取控制器中的數據,然後使用這個$ id變量在標記中使用。 –

+0

所以我不需要通過身份證在路線吧?如果我沒有通過它會產生錯誤 – bharathi

+0

不,你必須在路由以及控制器公共職能顧問($ id)這樣傳遞id ..那麼你的邏輯是在這裏從數據庫中獲取基於這個id的數據和然後在重定向到查看之後。 –

回答

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\[email protected]')->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> 
+0

解析錯誤:語法錯誤,意外的')',期待']'(查看:/home/gtpl/workspace/glob_hire/resources/views/Hr/view-request-candidates.blade.php) – bharathi

+0

更新答案。 –

+0

試圖獲取非對象的屬性 – bharathi

1

路線::

Route::get('home','[email protected]'); 

Route::get('view-hr-candidates/{hr_request_id}','[email protected]'); 

控制器::

<?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>