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

Amalmax's avatar

Undefined variable: users error

Hello, I am going to display My users table column data username and email in My index.blade.php file but when I visit it generate following error "Undefined variable: users" this is My UsersController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Auth;
use App\User;

use App\Http\Requests;

class UserController extends Controller
{
    public function index()
{
     $users = User::userr()->get();
    // $users = User::all();

    return view('users.index')->withUser($users);
}
}

User Model

<?php

namespace App;
use Auth;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'username', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
    
    



public function getAvatarUrl()
{
        return "http://www.gravatar.com/avatar/" . md5(strtolower(trim($this->email))) . "?d=mm&s=40";
}

public function scopeUserr($query)
{
     return $query->where('username',Auth::user()->id);
      
}
    
}

index.blade.php

@foreach($users as $use)
<h1>{{  $use->username } }</h1>
<h1>{{$use->email}}</h1>
@endforeach

how can fix this?

0 likes
46 replies
tomopongrac's avatar

You need this

    public function index()
{
     $users = User::userr()->get();
    // $users = User::all();

    return view('users.index')->withUsers($users); // <-- add "s" to User
}
tomopongrac's avatar

I didn't see but you must uncomment this

    public function index()
{
    // $users = User::userr()->get();
    $users = User::all();

    return view('users.index')->withUsers($users); // <-- add "s" to User
}

or you can try with

    public function index()
{
    // $users = User::userr()->get();
    $users = User::all();

    return view('users.index')->with('users', $users); // <-- add "s" to User
}
Amalmax's avatar

@tomi still generate same error, is this My index.blade.php file prob? I add this modification to My index. then error went away but index generate empty page no print username or email index.blade.php

 @if(isset($users))
@foreach($users as $use)
<h1>{{  $use->username } }</h1>
@endforeach
@endif
tomopongrac's avatar

Can you try this and see if you get any users

    public function index()
{
    // $users = User::userr()->get();
    $users = User::all();
    dd($users);

    return view('users.index')->with('users', $users); // <-- add "s" to User
}
tomopongrac's avatar

I think that you don't have any user in database so you need put in your blade file check like you post above

 @if(isset($users))
    @foreach($users as $use)
        <h1>{{  $use->username } }</h1>
    @endforeach
@endif
Amalmax's avatar

@tomi I think I have configured database correctly. if I use this function username and email print correctly in index.blade.php

<h>{{Auth::user()->username}}</h>
<h>{{Auth::user()->email}}</h>

but when I use this

<h1>{{  $use->username } }</h1>
<h1>{{$use->email}}</h1>

it did not display????

tomopongrac's avatar

Then i don't understand why your code for retreving users not working ...

M4rk3tt0's avatar

There is a space between the brackets. Should be

<h1>{{ $use->username }}</h1>
M4rk3tt0's avatar

Maybe it's a problem in your scope

Schould be:

public function scopeUserr($query)
{
     return $query->where('id',Auth::user()->id); // Not 'username'
      
}
PeterPan's avatar

Try,

 $users = \App\User::all();

Need to add the \App\ infront when calling user...

Perhaps..

Jaytee's avatar

@PeterPan OP has already imported the user model so no need for that.

I'm on my phone so apologies for formatting. In your index method, try:

$users = User::all();

return view('your view', compact('users'));

giovanniciriello's avatar

Does the query returns any data? Have you tried debugging $users variable before returning view?

Jaytee's avatar

Just to make sure, you are using the views/users/index.blade.php file right?

Amalmax's avatar

@zachleigh when I used your way it is generated following error

Undefined variable: users (View: C:\Users\max\Desktop\c\resources\views\users\index.blade.php)
Jaytee's avatar

Could you post your routes please? Just to see if there are duplicates.

Also try to delete the controller and view and re do it

Amalmax's avatar

routes.php

Route::get('/index', function(){
    return view('users.index');
});

User Model

<?php

namespace App;
use Auth;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'username', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
    
    



public function getAvatarUrl()
{
        return "http://www.gravatar.com/avatar/" . md5(strtolower(trim($this->email))) . "?d=mm&s=40";
}

public function scopeUserr($query)
{
     return $query->where('username',Auth::user()->id);
      
}
    
}

zachleigh's avatar
return view('users.index');

No users.

Try this:

Route::get('/index', function(){
    $users = User::all();
    return view('users.index',  ['users' => $users]);
});
Next

Please or to participate in this conversation.