laravel 如果有重复值,则突出显示行

nwsw7zdq  于 2023-01-06  发布在  其他
关注(0)|答案(2)|浏览(201)

您好,我有这些列"名称","电子邮件","用户名"和"ip"如果记录具有相同的ip,我将如何检查或突出显示刀片中的表行?
这是我的控制器

$promo = Promo::with(['participants' => function ($q) {
            $q->orderBy('winner', 'desc')->orderBy('created_at', 'desc');
        }])->find($id);

这是我的剑

@if($promos->participants->count() > 0)
    @foreach($promos->participants as $participant)
    <table>
      <tr class="align-middle">
        <td>Name</td>
        <td>Email</td>
        <td>Username</td>
        <td>IP</td>
      </tr>
      <tr>
        <td>{{$participant->name}}</td>
        <td>{{$participant->email}}</td>
        <td>{{$participant->username}}</td>
        <td>{{$participant->ip}}</td>
      </tr>
    </table>
    @endforeach
@endforeach
wbgh16ku

wbgh16ku1#

另一种方法是利用Larvel的集合获取副本,并将它们传递到视图中。

$ips = $promos->participants->pluck('ip');
$duplicateIps = $ips->duplicates()->unique()->all();

所以在你的刀片里你只需要检查

@if($promos->participants->isNotEmpty())
@foreach($promos->participants as $participant)
<table>
  <tr class="align-middle">
    <td>Name</td>
    <td>Email</td>
    <td>Username</td>
    <td>IP</td>
  </tr>
  <tr @class(['duplicate-row' => in_array($participant->ip, $duplicateIps)]>
    <td>{{$participant->name}}</td>
    <td>{{$participant->email}}</td>
    <td>{{$participant->username}}</td>
    <td>{{$participant->ip}}</td>
  </tr>
</table>
@endforeach
e5njpo68

e5njpo682#

像这样做
$ipList将在foreach运行时存储IP,并不断检查它们是否存在。如果存在,则将类duplicate-row添加到<tr>

$ipList = [];
@foreach($promos->participants as $participant)
  <tr class="align-middle @if(in_array($participant->ip, $ipList)) duplicate-row @endif">
    <td>{{$participant->name}}</td>
    <td>{{$participant->email}}</td>
    <td>{{$participant->username}}</td>
    <td>{{$participant->ip}}</td>
  </tr>
  @if(!in_array($participant->ip, $ipList))
    <?php array_push($ipList, $participant->ip); ?>
  @endif
@endforeach

在CSS中,您可以添加

.duplicate-row {
  background-color: red;
}

相关问题