动态下拉列表显示重复值Laravel 5.2

时间:2017-12-01 14:34:01

标签: php dynamic controller laravel-5.2 laravel-blade

尝试将登录用户设置为下拉选择值,但是当我检查下拉值列表时,它会显示重复值,例如Admin.I正在从存储在数据库中的users表生成下拉值....请帮助解决此问题。

以下是用户和管理员的DropDown被选为当前用户。

enter image description here

用户控制器:

 $users = User::all();
 $current_user = Auth::user()->name; 

Laravel Blade中的动态下载..

<select name="recieved_by" class="form-control">
  <option value="{{ $current_user }}" selected>{{ $current_user }}</option>
  @foreach($users as $user)
     <option value="{{$user->name}}">{{ $user->name }}</option>
  @endforeach
</select>

3 个答案:

答案 0 :(得分:0)

在查询中使用 groupBy 删除重复项。

同时从查询中删除当前用户,以便它不会在您的选择下拉列表中重复,如下所示:

$current_user = Auth::user()->name; 
$users = User::where('name','!=',$current_user)->groupBy('name')->get();

答案 1 :(得分:0)

DECLARE @p0 Int = 1
DECLARE @p1 Int = 7
DECLARE @p2 BigInt = 1798609
-- EndRegion
SELECT [t2].[IsActiveStatus], [t0].[OpportunityLostReasonID] AS [OpportunityReasonId], [t0].[Comment] AS [Comments], [t0].[LostToCompetitorID] AS [CompetitorId], [t0].[DataSourceID] AS [SourceId], [t3].[OpportunityCrossSellOptionID], [t3].[OpportunityID], [t3].[CrossSellOptionID], [t3].[DistanceRank], [t3].[BusinessLineID], [t3].[UpdatedDate], (
    SELECT COUNT(*)
    FROM [dbo].[GetCrossSellOptionForOpportunity]([t0].[OpportunityID]) AS [t4]
    ) AS [value], [t0].[UpdatedDate] AS [LastUpdated]
FROM [Opportunity] AS [t0]
INNER JOIN [OpportunityAssignmentUserPage] AS [t1] ON [t0].[OpportunityID] = [t1].[OpportunityID]
INNER JOIN [OpportunityStatus] AS [t2] ON [t2].[OpportunityStatusID] = [t0].[OpportunityStatusID]
OUTER APPLY [dbo].[GetCrossSellOptionForOpportunity]([t0].[OpportunityID]) AS [t3]
WHERE (([t0].[OpportunityStatusID] = @p0) OR ([t0].[OpportunityStatusID] = @p1)) AND ([t0].[OpportunityID] = @p2)
ORDER BY [t0].[OpportunityID], [t2].[OpportunityStatusID]

在使用 $users = array(); //declare an array @foreach($users as $user) $users['name'] = $user->name; @endforeach $users = array_unique($users); 循环

之前

希望这会对你有所帮助。

请检查语法,因为我不是laravel开发人员

谢谢,

答案 2 :(得分:0)

<select name="recieved_by" class="form-control">

       @foreach($users as $user)
           @if($current_user==$user->name)
              <option value="{{ $current_user }}" selected>{{ $current_user }}</option>
           @endif
           <option value="{{$user->name}}">{{ $user->name }}</option>
       @endforeach

</select>