如何比较Auth :: check()和where子句

时间:2016-08-16 08:21:23

标签: php laravel laravel-5.2

我试图设置一个条件,我需要检查当前用户是否是文档的批准者。如果用户是批准者接受,则拒绝按钮将显示在视图中。

控制器:

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>

2 个答案:

答案 0 :(得分:1)

在documentSentForApproval函数的控制器中,将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

答案 1 :(得分:0)

如果要在视图中进行检查,请将此值从控制器传递到数组中的视图:

$data['permission'] = 10;

在你看来:

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