I am very new to Laravel and PHP, just trying to list all users in my view file like this:
JavaScript
x
@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:
JavaScript
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:
JavaScript
$user = User::all();
to:
JavaScript
$users = User::all();
And remove the single quotes around $users
:
JavaScript
return View::make('users.index', ['users' => $users]);