Only show button to loged-in user in Laravel

懵懂的女人 提交于 2019-12-23 18:03:51

问题


If I logged in as John, how can I show only the red button for John instead of Susan's one?

Test system environment: Win10, Laravel5.4, Mysql5.7.19.

<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

回答1:


From what I understand, you only want to show that button on the rows that belong to that user.

This is a slight assumption, but I assume there is some sort of user identifier on that job row - ideally something like a user_id that links the row to the associated user.

If that's the case then you can just use a simple if statement for that button.

Something like the below would work with what you have:

@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

If you don't use a user identifier and only store the username then something like the below would work - using the user_name (I assume this is unique?):

@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



回答2:


Use Laravel policies feature:

@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

or:

@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



回答3:


Adding Role system will make this more easier so consider adding role system



来源:https://stackoverflow.com/questions/46703535/only-show-button-to-loged-in-user-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!