You need this
public function index()
{
$users = User::userr()->get();
// $users = User::all();
return view('users.index')->withUsers($users); // <-- add "s" to User
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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?
Please or to participate in this conversation.