Laravel @foreach - invalid argument entered

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

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

But getting the error message "Invalid argument 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?

+4
source share
1 answer

$usersnot defined in your controller, but $useris. You are trying @foreachon a variable that is literally equal to a string '$users'. Change:

$user = User::all();

to:

$users = User::all();

And remove the single quotes around $users:

return View::make('users.index', ['users' => $users]);
+7
source

Source: https://habr.com/ru/post/1531678/


All Articles