2017-10-11 40 views
1

如何在下面的代碼中將@if語句插入僅顯示「紅色刪除操作按鈕」的Loged-in用戶?只顯示Laravel中的內嵌用戶 - 集體

enter image description here

{!! 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() !!} 

謝謝你在上級篇。

回答

1

您可以使用Auth::check()這樣的:

{!! 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> 
    @if (Auth::check()) 
     {!! 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 
    </div> 
{!! Form::close() !!} 
+0

謝謝你的回答。順便說一下,是否有任何驗證方法,如顯示用戶登錄和退出活動? – Magnetic

+0

現在沒有了,你所能做的就是如果用戶被認證,使用這個方法獲得當前用戶或當前認證的用戶ID''user = Auth :: user(); $ id = Auth :: id();'並且登錄一個用戶並進行更多認證的事情請看[documentation;)](https://laravel.com/docs/5.5/authentication) – Maraboc

+0

你能否回答這個問題鏈接? https://stackoverflow.com/questions/46703535/only-show-button-to-loged-in-user-in-laravel – Magnetic

1

我認爲你在尋找這樣的事情:

<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> 
    @if(Auth::check()) 
     {!! Form::open(['route' => ['jobs.destroy', $job->id], 'method' => 'delete']) !!} 
      {!! Form::button('<i class="glyphicon glyphicon-stop"></i>', ['type' => 'submit', 'class' => 'btn btn-danger btn-xs', 'onclick' => "return confirm('Are you sure to Stop?')"]) !!} 
     {!! Form::close() !!} 
    @endif 
</div> 

請注意,我已經動了你的形式if語句裏面你在這裏,如果您仍然希望其他按鈕被包裹在表格內,請使用以下代碼

{!! 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> 
     @if(Auth::check()) 
      {!! 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 
    </div> 
{!! Form::close() !!}