Skip to content
Advertisement

Laravel @foreach – invalid argument supplied

I am very new to Laravel and PHP, just trying to list all users in my view file like this:

@foreach ($users as $user)
    <li>{{ link_to("/users/{$user->username}", $user->username) }}</li>
@endforeach

But getting an error which says ‘Invalid argument supplied for foreach()’

In my controller, I have the following function:

public function users() {
    $user = User::all();
    return View::make('users.index', ['users' => '$users']);
}

What am I doing wrong?

Advertisement

Answer

$users is not defined in your controller, but $user is. You are trying to @foreach over a variable that literally equals the string '$users'. Change:

$user = User::all();

to:

$users = User::all();

And remove the single quotes around $users:

return View::make('users.index', ['users' => $users]);
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement