2016-08-16 52 views
0

我想设置一个条件,我需要检查当前用户,如果他/他是文档的批准者。如果用户是批准者接受拒绝按钮将显示到视图。如何比较身份验证::检查()基于哪里条款

控制器:

public function documentsSentForApproval() 
{ 

    $pendingDocumentLists = DB::table('approve_document')->select('documents.title', 'documents.content', 
     'categories.category_type', 'users.username', 'approve_document.dateReceived', 'documents.id') 
    ->join('documents', 'documents.id', '=', 'approve_document.document_id') 
    ->join('categories', 'categories.id', '=', 'documents.category_id') 
    ->join('approves', 'approves.id', '=', 'approve_document.approve_id') 
    ->join('users', 'users.id', '=', 'approves.approver_id') 
    ->where('approver_id', '=', Auth::id()) 
    ->orWhere('requestedBy', '=', Auth::id()) 
    ->get(); 

    return view ('document.pending') 
    ->with('pendingDocumentLists', $pendingDocumentLists); 
} 

正如你可以在这里看到的where子句approver_id的是谁要去批准文件之一。我只是在requestedBy列是创建批准文档的那个小问题。

逻辑:

我需要检查,如果当前用户!= approver_id。如果不是接受拒绝将不会显示到视图。

查看:

<table class = "table"> 

    <thead> 
     <tr> 
      <th>Title</th> 
      <th>Content</th> 
      <th>Category</th> 
      <th>Approver</th> 
      <th>Date Sent</th> 
      <th>Action</th> 
      <th>Status</th> 
     </tr> 
    </thead> 

    <tbody> 
     @foreach ($pendingDocumentLists as $list) 
      <tr class = "info"> 

       <td>{{ $list->title }}</td> 
       <td>{{ strip_tags(substr($list->content, 0, 50)) }} {{ strlen($list->content) > 50 ? "..." : '' }}</td> 
       <td>{{ $list->category_type }}</td> 
       <td>{{ $list->username }}</td> 
       <td>{{ date('M, j, Y', strtotime($list->dateReceived)) }}</td> 
       <td> 
        <a href = ""><button type = "submit" class = "btn btn-primary glyphicon glyphicon-open"> Read</button></a> 
        <button type = "submit" class = "btn btn-danger glyphicon glyphicon-trash"> Delete</button> 
        @if (Auth::check() ==)<!--I'm stuck here--> 
         <button type = "submit" class = "btn btn-success glyphicon glyphicon-thumbs-up"> Approve</button> 
         <button type = "submit" class = "btn btn-warning glyphicon glyphicon-thumbs-down"> Reject</button> 
        @endif 
       </td> 
      </tr> 
     @endforeach 
    </tbody> 

用户界面:

enter image description here

更新

我加入$data['permission'] = ['approver_id'];作为@Mayank说,这是我在我看来所做的。

<tbody> 
    @foreach ($pendingDocumentLists as $list) 
     <tr class = "info"> 

       <td>{{ $list->title }}</td> 
       <td>{{ strip_tags(substr($list->content, 0, 50)) }} {{ strlen($list->content) > 50 ? "..." : '' }}</td> 
       <td>{{ $list->category_type }}</td> 
       <td>{{ $list->username }}</td> 
       <td>{{ date('M, j, Y', strtotime($list->dateReceived)) }}</td> 
       <td> 
      @if ($permission != Auth::id()) 
       <a href = ""><button type = "submit" class = "btn btn-primary glyphicon glyphicon-open"> Read</button></a> 
       <button type = "submit" class = "btn btn-danger glyphicon glyphicon-trash"> Delete</button> 
      @else 
       <button type = "submit" class = "btn btn-success glyphicon glyphicon-thumbs-up"> Approve</button> 
       <button type = "submit" class = "btn btn-warning glyphicon glyphicon-thumbs-down"> Reject</button> 
      @endif 
      </td> 
     </tr> 
    @endforeach 
</tbody> 
+0

不能使用$列表 - > approve_id ==验证:: ID()? – KmasterYC

+0

尝试将其添加到选择语句 – KmasterYC

+0

它已经工作。我刚刚在我的选择中添加了“approves.approves_id”。 – Francisunoxx

回答

1

在您的控制器在DocumentsSentForApproval函数中添加approver_id至我们的选择

public function documentsSentForApproval() 
{ 

    $pendingDocumentLists = DB::table('approve_document')->select('documents.title', 'documents.content', 
     'categories.category_type', 'users.username', 'approve_document.dateReceived', 'documents.id', 'approves.approver_id') 
    ->join('documents', 'documents.id', '=', 'approve_document.document_id') 
    ->join('categories', 'categories.id', '=', 'documents.category_id') 
    ->join('approves', 'approves.id', '=', 'approve_document.approve_id') 
    ->join('users', 'users.id', '=', 'approves.approver_id') 
    ->where('approver_id', '=', Auth::id()) 
    ->orWhere('requestedBy', '=', Auth::id()) 
    ->get(); 

    return view ('document.pending') 
    ->with('pendingDocumentLists', $pendingDocumentLists); 
} 

您认为这些文件,你可以这样做:

@if (Auth::id() == $list->approver_id) 
    <button type = "submit" class = "btn btn-success glyphicon glyphicon-thumbs-up"> Approve</button> 
    <button type = "submit" class = "btn btn-warning glyphicon glyphicon-thumbs-down"> Reject</button> 
@endif 
+0

嘿什么好的解决方案没有注意到这将工作。感谢你! :) – Francisunoxx

0

如果你想看看你的观点不是像数组从您的控制器此值传递到您的视图:

$data['permission'] = 10; 

在您的看法:

if($permission != Auth::id()) 
{ 
    // show option or what ever you want 
} 
else 
{ 
    // dont show option or what ever you want 
} 
+0

为什么我应该把$数据作为10? – Francisunoxx

+0

只是例如兄弟:) –

+0

我有点混淆了什么应该是我的'$ data'的价值:p – Francisunoxx