2017-10-12 11 views
3

私がJohnとしてログインしている場合、Susanさんの代わりにJohnの赤いボタンのみを表示するにはどうすればよいですか?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

@urfusion、まだロールシステムを使用していません。ありがとう。 – Magnetic

+0

こんにちは2つの違いは何ですか? – 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)"のように変更しました。 hava良い一日!ありがとう。 – 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ポリシーを最初に作成する必要があります。ドキュメントをお読みください。そして '$ post'の代わりに' $ job'を使います。 '$ post'はドキュメントの一例です。 –

+1

あなたの助言に感謝します。私はポリシー機能の使用について学ぶ必要があります:) – Magnetic

0

役割システムを追加すると、これは、より簡単になりますので、ジョンとスーザンの両方が異なる役割を持っている

+0

okポリシーを調整しようとした後で調整します。ありがとう。) – Magnetic

関連する問題