Amalmax
1001
46
Laravel

Undefined variable: users error

Posted 2 years ago by Amalmax

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 sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.