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

GuntarV's avatar
Level 40

Assign variable inside blade @foreach loop

I have this simplified code example below (unfortunately it is not working):

@php 
	$department = null;  
@endphp

@foreach ($users as $user)
	<h1 class = "{{ $department === $user->department ? 'red' : 'blue'">{{ $user->name }} </h1>
	@php 
		$department = $user->department;  
	@endphp
@endforeach

$users collection sent to view is ordered by department. I would like to alternate the color for each department on the list.

The example I have above is not working. The way it is written Laravel blade is not liking the variable assignment before @endforeach, I get the squiggly lined under $user.

What would be the correct approach for assigning $user->department to the variable above the @foreach loop?

0 likes
12 replies
bugsysha's avatar

Use OOP to your advantage.

@foreach ($users as $user)
	<h1 class = "{{ $user->department->getColor() }}">{{ $user->name }} </h1>
@endforeach

Then in the department add that method:

public function getColor()
{
  return $this->something === 'something' ? 'red' : 'blue';
}

This breaks SRP but it is far better than what you wanted to do.

GuntarV's avatar
Level 40

Hmm..., I would like to alternate between red and blue only and there could be many different departments. In the loop I need to switch color every time the department is different.

Somehow the loop need to keep track what was the department for previous $user.

bugsysha's avatar
public function getColor()
{
  return [
    'a' => 'red',
    'b' => 'blue',
    'c' => 'green',
    // and so on...
  ][$this->something];
}
GuntarV's avatar
Level 40

The thing is I don't know what the department names are going to be, therefore, I cannot hard code them.

This is for SASS application

bugsysha's avatar

Then do not make decision based on the name. Key in the array can be different things.

CorvS's avatar

@guntarv Just as side note, but if all you want to do is alternating the colors it's more efficient to do it using CSS imo:

<h1 class="foo">...

// CSS
.foo:nth-child(odd) {
    color: red;
}

.foo:nth-child(even) {
    color: blue;
}

No need for extra queries or whatever.

MichalOravec's avatar

Or you can use $loop variable

@foreach ($users as $user)
	<h1 class="{{ $loop->even ? 'red' : 'blue' }}">{{ $user->name }}</h1>
@endforeach
GuntarV's avatar
Level 40

If I user the $loop->even that will alternate every <h1> In the $user collection there could be first 7 user with one department, next 2 user with different and so on... I wan the color to be different only if there is a different department.

GuntarV's avatar
Level 40

Yep, but unfortunately I don't know what the department names are going to be, therefore I cannot hardcode them.

Snapey's avatar
Snapey
Best Answer
Level 122

your ide does not recognise blade foreach, so, although your code is valid, your ide does not know that

Please or to participate in this conversation.