Laravel-在索引视图刀片上显示0的“否”和1的“是”

时间:2019-12-29 17:01:52

标签: laravel

我正在Laravel-5.8中开发一个项目。

控制器

public function index()
{
        $identities = AppraisalIdentity::where('company_id', $userCompany)->get();
    return view('appraisal.appraisal_identities.index')->with('identities', $identities);
}

index.blade.php

   <tbody>
  @foreach($identities as $key => $identity)
    <td>
      {{$key+1}}
    </td>
    <td>
       {{$identity->appraisal_name ?? '' }}
    </td>
    <td>
       {{ $identity->is_current ?? '' }}
    </td>
  @endforeach 

我想要实现的是,当is_current为0时,它应该显示否为红色,当is_current为1时,它应该显示为绿色。< / p>

我该如何实现?

谢谢

2 个答案:

答案 0 :(得分:1)

您可以为此使用Blade未转义的括号符号来添加具有以下样式或类的css span:

{!! $identity->is_current ? "<span class='green'>Yes</span>" : "<span class='red'>No</span>" !!}}

红色和绿色是您的颜色的样式类。

答案 1 :(得分:0)

您可以尝试使用Laravel blade's if statement

<td>
    @if ($identity->is_current)
        <span style="color: green;">Yes</span>
    @else
        <span style="color: red;">No</span>
    @endif
</td>