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

sergionc's avatar

Invalid argument supplied for foreach with livewire

Greetings friends, I have a problem and I cannot understand. I am using livewire, I have a button that when I click it brings me to all the users

class AsignarNota extends Component { public $usuarios;

public function render()
{
	$cursos = Curso::all();
	return view('livewire.asignar-nota',compact('cursos'));
}

public function usuarios()
{
	$this->usuarios = User::all();
}

}

		<tbody>
			<tr>
				@foreach ($usuarios as $usuario)
				<th scope="row">{{ $usuario->id }}</th>
				@endforeach
			</tr>
		</tbody>

Invalid argument supplied for foreach().

0 likes
1 reply
Nakov's avatar
Nakov
Best Answer
Level 73

I would guess that it is because you have: public $usuarios; which by default is null and foreach throws an exception not being able to iterate over a null object. Don't know or see where and how do you call the usarios method which adds the users to the list.

Maybe a solution would be if you initialize it as an empty array, and fill it in in the constructor as I don't see you doing anything special in the document.

So try this:

class AsignarNota extends Component { 

    public $usuarios;

    public function __construct()
    {
	$this->usuarios = User::all();
    }

    public function render()
    {
	$cursos = Curso::all();
	return view('livewire.asignar-nota',compact('cursos'));
    }
   
}

or have this public $usarios = []; at least.

1 like

Please or to participate in this conversation.