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

zachleigh's avatar

Yes, but you are not passing the view any users.

1 like
Jaytee's avatar

Your route is the problem, I knew that was why. Make it like this :

Route::get('/index', 'UserController@index');

1 like
Jaytee's avatar

Then reason it wasn't working is because you were just telling your route to return the view and not call the controller and then method.

The controller method would then be responsible for handling the logic you've set and returning a possible view back to the route

Amalmax's avatar

@Jaytee did your comment but got this error

Class App\Http\Controllers\UsersController does not exist
Jaytee's avatar

I refreshed comment, it should be UserController in your case not UsersController

Amalmax's avatar

@Jaytee did as your comment. but not display variables data. only display empty page

Jaytee's avatar

Check your index method and make sure it's still returning a view in your user controller.

Then make sure your route is this;

Route::get('/index', 'UserController@index');

If you still don't see anything then you don't have any users

1 like
Jaytee's avatar

Post a picture if your view directory, the actual view itself , user model, routes and controller please. It's clear that your route was the problem so something has been changed

Amalmax's avatar

well this is My scripts again, UserController.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();
    
 return view('users.index')->withUser($users);
   
}

}

User.php 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

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


@endif  

routes.php

<?php
Route::get('/index', 'UserController@index');

My table name is "users" and I have "id", "username", "email", "password", "avatar_url" columns in My table.

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

Shouldn't this be checking the username not the id?

public function scopeUserr($query)
{
     return $query->where('username',Auth::user()->username);
      
}
1 like
zachleigh's avatar

There are so many misspelled words and typos... To start:

->withUser($users);

Should be

->withUsers($users);
1 like
Amalmax's avatar

well, at last I got results. thanks for reply. help me following comments

@Jaytee

Route::get('/index', 'UserController@index');

@zachleigh

->withUsers($users);

and @bonsai

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

I dont know what is the best answer all are important me Thanks all.

Jaytee's avatar

That's good finally however your scope is only ever going to return one user since you're checking against the authenticated user. In that case, if you just want the authenticated user, delete the scope, the foreach loop in your view and just use the auth facade to get the user.

If you want all users, use the all method on the users model

1 like
Amalmax's avatar

Ok, now I am going to edit My users data can you give me some comments about edit method and routes

Previous

Please or to participate in this conversation.