Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

benxmy@gmail.com's avatar

Change row color when variable value is different in loop in Blade template

I have a report page that loops through a collection and displays the data in a generic bootstrap template table - it's showing data about users, and one user can have multiple rows in the table. I group the rows by the user ID and I want to basically stripe the table rows based on the user ID (not just alternating rows). So, for example, I might display 4 rows for user ID = 5 with a white background and then 2 rows for user ID = 6 with a gray background. I'd just use a local variable in straight php, but is there an elegant/clean way to do this in the blade templates?

@foreach($payments->getDetails() AS $k=>$detail)
<tr>
    <td>{{ $detail->payment->userid }}</td>
    <td>${{ $detail->payment->amount }}</td>
    <td>{{ $detail->payment->status }}</td>
</tr>
@endforeach 

In this example, $payments is a collection of $paymentsDetails objects. So based on the $detail->payment->userid value I'd like to determine the class or background color.

Thank you for any help in this.

0 likes
5 replies
zachleigh's avatar

You could just use conditionals:

@foreach($payments->getDetails() AS $k=>$detail)
<tr>
    @if ($detail->payment->userid === 4)
        <td class="grey">{{ $detail->payment->userid }}</td>
    @else
        <td>{{ $detail->payment->userid }}</td>
    @endif
    <td>${{ $detail->payment->amount }}</td>
    <td>{{ $detail->payment->status }}</td>
</tr>
@endforeach 

That would be fine for one or two different colors. If you want more, it would get messy. In that case, I'd probably figure it out in the controller and then assign it to the td in the view.

Cronix's avatar
Cronix
Best Answer
Level 67

I'd do it the way you were thinking originally and just use @php/@endphp blade directives to determine if the userid has changed during each loop iteration and if it does change the color.

@zachleigh That won't work for a variable number of users since you're hardcoding the userid. You'd have to have an if statement for each userid, which is presumably dynamic.

@php 
$lastid = null;
$rowclass = 'grey';
@endphp

@foreach($payments->getDetails() AS $k=>$detail)
@php 
 //if userid changed from last iteration, store new userid and change color
 if ($lastid !== $detail->payment->userid)
 {
    $lastid = $detail->payment->userid;
    if ($rowclass == 'grey') $rowclass = 'white';
    else $rowclass = 'grey';
 }
@endphp
<tr class="{{ $rowclass }}">
    <td>{{ $detail->payment->userid }}</td>
    <td>${{ $detail->payment->amount }}</td>
    <td>{{ $detail->payment->status }}</td>
</tr>
@endforeach 
4 likes
zachleigh's avatar

@Cronix Yes, it would have to be dynamic...I was just giving an example of the Blade structure. Rather then have all the php tags in the view, I would simply assign a $userId variable in the controller and send it to the view.

@foreach($payments->getDetails() AS $k=>$detail)
<tr>
    @if ($detail->payment->userid === $userId)
        <td class="grey">{{ $detail->payment->userid }}</td>
    @else
        <td>{{ $detail->payment->userid }}</td>
    @endif
    <td>${{ $detail->payment->amount }}</td>
    <td>{{ $detail->payment->status }}</td>
</tr>
@endforeach 
benxmy@gmail.com's avatar

@zachleigh - I don't believe that solution will work because the loop has to continue to check the previous userid - so, for example, there could be 500 different user ids in 2000 rows, and it needs to alternate every time there's a different userid than the previous. I'd prefer to use the elegance that you have in your example, but I couldn't come up with a way to do it in the way you showed in the example. Let me know if I'm missing something though!

1 like

Please or to participate in this conversation.