只在Laravel中显示loged-in用户的按钮 [英] Only show button to loged-in user in Laravel
问题描述
如果我以John身份登录,我怎样才能显示John的红色按钮而不是Susan的红色按钮?
If I logged in as John, how can I show only the red button for John instead of Susan's one?
测试系统环境: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
推荐答案
据我了解,您只想在属于该用户的行上显示该按钮。
From what I understand, you only want to show that button on the rows that belong to that user.
这是一个轻微的假设,但我假设该作业上有某种用户标识符行 - 理想情况下类似 user_id
将行链接到关联用户。
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语句。
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
如果您不使用用户标识符并仅存储用户名,那么类似下面的内容将起作用 - 使用user_name(我认为这是唯一的?):
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
这篇关于只在Laravel中显示loged-in用户的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!