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

option's avatar

Get Users Information

So i have created a users online page and each username is a link to their profiles however,

I cannot quite figure out how to do this..

Here is my Controller:

public function user(User $id)
    {
        $profile = User::findOrFail($id)->all();

        return view('profile.profile', compact('profile'));
    }

Here is my route:

Route::get('/profile/{id}', [
                'as' => 'id',
                'uses' => 'ProfileController@user'
            ]);

For some reason I can't echo out the users information yet, when I var_dump it, it shows everything I am trying to get..

0 likes
12 replies
36864's avatar
$profile = User::findOrFail($id)->all();

This is returning every user in your database. Drop the all()

$profile = User::findOrFail($id);

1 like
option's avatar

@3684, This doesn't seem to pull out the information I need now when I var_dump

option's avatar

Oh i see!

$profile = User::findOrFail($id)->get('id', $id);

seems to do the trick

1 like
36864's avatar

Apologies, I didn't read your method signature and assumed $id was just and id.

public function user(User $id)

means Laravel will inject a User instance into your function, not pass an id. It might be best to change the name of that variable to $user to avoid confusion. From your code, I assume your view is expecting a User instance. If that's the case, this should work:

public function user(User $user)
{
return view('profile.profile', compact('user'));
}
option's avatar

Would you suggest my working method isn't really the best route to go?

36864's avatar

Laravel already fetches the user for you. What you're doing is just fetching it again. You might want to read up on laravel's dependency injection here: https://laravel.com/docs/5.4/controllers#dependency-injection-and-controllers

Basically, the way you have things set up, you're telling Laravel that you're expecting a parameter to be passed into your route, and that that parameter is a User instance. When a user hits that route, Laravel will attempt to find a user with a matching id and pass it into your controller. So, in the end, what's actually happening in your code is something like this:

$id = User::find($request->get('id'));

$profile = User::findOrFail($id->id);

Just to clear everything up, could you specify what you want to happen if a user id is not passed into your route?

Do you have a separate route for listing the users, such as

Route::get('/profiles/', 'UserController@index');
option's avatar

Sorry ill explain my intentions somewhat more.

So basically I have an online users page which will show every username online along with the count (this is functional and runs off of sessions table stored within the database).

On each username there's a hyperlink to the users profile (/profile/username) but I have it currently set to their ID instead to display their information ie: username, email address etc.

This is the code above that I posted to gather all of X users details to then display on their profile page.

36864's avatar

When you say "gather all of X user details", it leads me to believe you might not fully understand how the model works. When you have a User instance, all of its attributes are visible to you. You're trying very hard to do something that you don't need to do, which is "fetch the profile data". You already have it in the user instance.

$profile = User::findOrFail($id)->get('id', $id);
echo $profile->name;

is the same as

echo $id->name;

because $id is an instance of User, as it is being type-hinted in the function signature.

What you're doing isn't necessarily wrong, but it is unnecessary and at the very least is making needless database requests.

Either I'm massively misunderstanding your intentions, or this would work exactly the way you want it to:

public function user(User $profile)
{
return view('profile.profile', compact('profile'));
}

and change your route's parameter name:

Route::get('/profile/{profile}', [
                'as' => 'id',
                'uses' => 'ProfileController@user'
            ]);

Alternatively, if you really don't want to change your variable name,

public function user(User $id)
{
$profile = $id;
return view('profile.profile', compact('profile'));
}

Sorry for having gone back and forth so much on this, I got very confused about the misnamed $id variable (in your controller, it's not an id, it's an object of type App\User)

option's avatar

@eskiesirius, I figured the issue and now have it functioning as you have mentioned. My only question to extend this now would be, rather than it visiting the id of the user, how would I make it go to /profile/{username} instead using the same function within the controller?

36864's avatar

Straight from the documentation, which @eskiesirius linked for you:

Customizing The Key Name

If you would like model binding to use a database column other than id when >retrieving a given model class, you may override the getRouteKeyName >method on the Eloquent model:

/**
* Get the route key for the model.
*
* @return string
*/
public function getRouteKeyName()
{
   return 'slug';
}  

So, in your case add this to your User model:

public function getRouteKeyName()
{
return 'username';
}
1 like
option's avatar

Oh yes, silly me >_< Sorry hah!

Please or to participate in this conversation.