1

How to insert @if statement for only show the "red color delete action button" to loged-in user in below code?

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

thank you in advenced.

Community
  • 1
  • 1
JsWizard
  • 1,663
  • 3
  • 21
  • 48

2 Answers2

1

You can use Auth::check() like this :

{!! 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() !!}
Maraboc
  • 10,550
  • 3
  • 37
  • 48
  • Thank you your answer. By the way, is there have any Auth method like show user login and out activities? – JsWizard Oct 11 '17 at 15:03
  • For now there isn't, all you can do is if the user is authenticated get the current user or the current authenticated user id using this methods `$user = Auth::user(); $id = Auth::id();` and log a user in and more authentication things see [documentation ;)](https://laravel.com/docs/5.5/authentication) – Maraboc Oct 11 '17 at 15:23
  • could you answer to this question link? https://stackoverflow.com/questions/46703535/only-show-button-to-loged-in-user-in-laravel – JsWizard Oct 12 '17 at 07:13
  • @Magnetic let us have a chat about this problem [here](https://chat.stackoverflow.com/rooms/156528/problem-solution) – Maraboc Oct 12 '17 at 07:51
1

I think you're looking for something like this:

<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>

Note that I have moved your form inside of the if statement for you here, if you'd still like the other buttons to be wrapped inside a form use the following code

{!! 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() !!}
J0sh0nat0r
  • 211
  • 2
  • 8