我正在尝试对数组元素的值使用条件来为div设置背景色。

问题是我的语法在某处是错误的,因为正确的颜色正在转储带有周围的标签,但实际上并未将div设置为正确的颜色。所以看来条件在起作用,但我的语法在某处坏了

@foreach($placedList as $placed)
    @if ($placed['AGING'] < 30)
       $color = "green";
    @elseif ($placed['AGING'] > 30 and $placed['AGING'] < 60)
       $color = "yellow"
    @else
       $color = "red";
    @endif

    <div class="uk-width-1-10" style="background-color:{{$color}}">{{$placed['AGING']}}</div>

@endforeach


要实际应用需要更改什么?

最佳答案

用以下代码替换您的代码

@foreach($placedList as $placed)

    @php

    if ($placed['AGING'] < 30) {
       $color = "green";
    }elseif ($placed['AGING'] > 30 and $placed['AGING'] < 60) {
       $color = "yellow";
    }else{
       $color = "red";
    }

    @endphp

    <div class="uk-width-1-10" style="background-color:{{$color}}">{{$placed['AGING']}}</div>

@endforeach

10-08 15:49