Yes, but you are not passing the view any users.
Your route is the problem, I knew that was why. Make it like this :
Route::get('/index', 'UserController@index');
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
@Jaytee did your comment but got this error
Class App\Http\Controllers\UsersController does not exist
I refreshed comment, it should be UserController in your case not UsersController
@Jaytee did as your comment. but not display variables data. only display empty page
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
@jaytee I have 5 users in My users table
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
@jaytee dear, do you know how can post a picture via this thered?
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.
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);
}
There are so many misspelled words and typos... To start:
->withUser($users);
Should be
->withUsers($users);
well, at last I got results. thanks for reply. help me following comments
Route::get('/index', 'UserController@index');
->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.
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
Ok, now I am going to edit My users data can you give me some comments about edit method and routes
Please or to participate in this conversation.