2017-10-12 68 views
3

如果我以約翰身份登錄,如何僅顯示John的紅色按鈕而不顯示Susan的紅色按鈕?僅在Laravel中顯示按鈕以便內置用戶

測試系統環境:Win10,Laravel5.4,Mysql5.7.19。

enter image description here

<table class="table table-responsive" id="jobs-table"> 
    ... 
    @foreach($jobs as $job) 
     <tr> 
      <td>{!! $job->user_name !!}</td> 
      ... 
      <td>{!! $job->created_at !!}</td> 
      <td> 
       {!! Form::open(['route' => ['jobs.destroy', $job->id], 'method' => 'delete']) !!} 
       <div class='btn-group'> 
        <a href="{!! route('jobs.show', [$job->id]) !!}" class='btn btn-default btn-xs'><i class="glyphicon glyphicon-eye-open"></i></a> 
        <a href="{!! route('jobs.edit', [$job->id]) !!}" class='btn btn-default btn-xs'><i class="glyphicon glyphicon-edit"></i></a> 
        {!! Form::button('<i class="glyphicon glyphicon-stop"></i>', ['type' => 'submit', 'class' => 'btn btn-danger btn-xs', 'onclick' => "return confirm('Are you sure to Stop?')"]) !!} 
       </div> 
       {!! Form::close() !!}     
      </td> 
     </tr> 
    @endforeach 
+0

既約翰和蘇珊都有不同的角色? – urfusion

+0

@fusion,還沒有使用角色系統。謝謝。 – Magnetic

+0

嗨兩者之間有什麼區別? – Maraboc

回答

2

據我所知,你只想顯示在屬於該用戶的行該按鈕。

這是一個輕微的假設,但我認爲在該作業行上有某種用戶標識符 - 理想情況下類似於將該行鏈接到關聯用戶的user_id

如果是這種情況,那麼你可以使用該按鈕的簡單的if語句。

喜歡的東西下面將與你有什麼工作:

@if ($job->user_id == Auth::id()) 
    {!! Form::button('<i class="glyphicon glyphicon-stop"></i>', ['type' => 'submit', 'class' => 'btn btn-danger btn-xs', 'onclick' => "return confirm('Are you sure to Stop?')"]) !!} 
@endif 

如果你不使用的用戶標識,只存儲用戶名,然後像下面將工作 - 使用USER_NAME(我假設這是唯一的):

@if ($job->user_name == Auth::user()->user_name) 
    {!! Form::button('<i class="glyphicon glyphicon-stop"></i>', ['type' => 'submit', 'class' => 'btn btn-danger btn-xs', 'onclick' => "return confirm('Are you sure to Stop?')"]) !!} 
@endif 
+0

非常感謝您的詳細解答,但是在這兩種情況下,它都是無形的紅色按鈕。我也檢查了不同的用戶登錄。:( – Magnetic

+0

所以我修改了「if($ job-> user_name == Auth :: user() - > name)」。你的想法是正確的:)。祝你有美好的一天! – Magnetic

3

使用Laravel policies功能:

@can('delete', $job) 
    {!! Form::button('<i class="glyphicon glyphicon-stop"></i>', ['type' => 'submit', 'class' => 'btn btn-danger btn-xs', 'onclick' => "return confirm('Are you sure to Stop?')"]) !!} 
@endcan 

或:

@if (auth()->user()->can('delete', $job)) 
    {!! Form::button('<i class="glyphicon glyphicon-stop"></i>', ['type' => 'submit', 'class' => 'btn btn-danger btn-xs', 'onclick' => "return confirm('Are you sure to Stop?')"]) !!} 
@endif 
+0

謝謝。但結果是(3/3)ErrorException 未定義的變量:$ post。我如何在這段代碼中使用「can」語句? – Magnetic

+0

@Magnetic您需要首先創建一個策略,請閱讀文檔。然後使用'$ job'而不是'$ post'。 '$ post'只是文檔中的一個例子。 –

+1

謝謝你的建議。我需要了解使用策略功能。:) – Magnetic

0

添加角色系統將會使這更容易因此考慮增加角色系統

+0

ok我會在嘗試調整策略之後調整。謝謝。;) – Magnetic

相關問題