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

NoTimeForCaution's avatar

Livewire Blade Syntax

Blade

<div wire:init="loadCachedCollections"></div>

Livewire

public $upgrades;

public function loadCachedCollections()
    {
        $this->upgrades = $award->upgrades(); // below 

	// upgrades()
        return collect($awards->where('upgrade', 1))->groupBy('award_fleet');
    }

Output to blade

{{ $upgrades }} // displays the data

But @foreach in blade

@foreach($upgrades as $k => $v)
@endforeach

Results in

foreach() argument must be of type array|object, null given 

Any wrapping of the $upgrades variable in the blade file returns null.

0 likes
2 replies
Snapey's avatar
Snapey
Best Answer
Level 122

in your function

public function loadCachedCollections()
    {
        $this->upgrades = $award->upgrades(); // below 

	// upgrades()
        return collect($awards->where('upgrade', 1))->groupBy('award_fleet');
    }

the final statement does nothing since your function need not return anything.

You will have an issue if you do not initialise $upgrades to an empty collection since render will be called before you load the data.

NoTimeForCaution's avatar

My apologies. I included that return fragment for brevity. It's not in the loadCachedCollections() function.

LIVEWIRE

public $upgrades;

public function loadCachedCollections()
{
	$award = new VacancyAwards;
	$this->upgrades = $award->upgrades();
}

Vacancy Awards Class

public function upgrades()
{
	return collect($awards->where('upgrade', 1))->groupBy('award_fleet');
}

But you still got it. I initialized $upgrades as an empty collection on mount and it worked. Thanks.

Please or to participate in this conversation.