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

sanjayacloud's avatar

Cannot get login user data

Hi, I am try to get login user data. But I got "Trying to get property of non-object" error..

This my controller

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

    }

This my Route

Route::get('/profile/{user}','ProfileController@user');
0 likes
28 replies
tykus's avatar

If you want route-model binding then the route wildcard must match the method parameter, i.e.

// Route
Route::get('/profile/{user}', 'ProfileController@user'); 

// Controller method
public function user(User $user)
{
    
}

But, do you need to pass a user id; if it is the authenticated user's profile, then I would suggest:

// Route
Route::get('/profile', 'ProfileController@user'); 

// Controller method
public function user()
{
    return auth()->user();
}
Cronix's avatar

The variable in the method must match the variable in the route. You used "{user}" in the route, so you need to use $user in the method.

Also, you're using route model binding, which means it will retrieve the user, so $user will be the user with the id that you passed in the route, like /profile/5. It is also doing findOrFail behind the scenes when using route model binding, so the equivalent of what you were trying to do would just be

    public function user(User $user)
    {
        dump($user);
    }

also use dump(), it's a lot better than var_dump() as it formats everything nicely.

sanjayacloud's avatar

@Cronix I as your advice, I have do like below. I have pass user id manually. I need this id get from currant login user. Because, This is a user profile. Can you help me to do this.

My method:


$user = User::findOrFail(7);
        dump($user);

Cronix's avatar

Use @tykus 2nd code

You don't have to retrieve/query the current logged in user again. The current logged in user is always available via Auth::user(), or auth()->user().

So you don't even need to pass an id in the route.

sanjayacloud's avatar

@Cronix Ok. But my question is how can I redirect to this profile page. Now I pass Id mannually in findOrFail() method like this. $user = User::findOrFail(7). How Can i replace this 7 to current login user id

tykus's avatar

How Can i replace this 7 to current login user id

You do not need to find the currently logged in user again - auth()->user() is the currently logged in user.

how can I redirect to this profile page

Why would you be redirecting anywhere - you are making a GET request to /profile? Just return a view if that is what your need

Cronix's avatar

How Can i replace this 7 to current login user id

You most likely don't need to. Let's first clarify something... Should a user only be able to see their own profile, or should a user be able to visit anybodies profile? That will clarify what needs to be done here.

sanjayacloud's avatar

@Cronix @tykus

I have done like this

  public function user()
    {
        $data = auth()->user();
        dump($data);
    }

But I got null. But I have already login this system. And my home controller show my user id,

tykus's avatar

If you're getting null then you are not authenticated.

Cronix's avatar

And the route is in web.php, and the route is

Route::get('/profile', 'ProfileController@user');

?

sanjayacloud's avatar

@tykus I have already authenticated.. And my home controller show all my user details what I print.. This happen in this controller...

@Cronix Yes. My Route is Route::get('/profile','ProfileController@user');

tykus's avatar

If you're getting null then you are no longer authenticated.

auth()->user() will return the authenticated user, or null if there is no authenticated user. This route should be inside the auth middleware group also.

Is this a web route or an api route?

Cronix's avatar

If you go to /login, does it show the login form? If so, you're not logged in.

tykus's avatar

So you are not authenticated like I said a number of posts ago.

Cronix's avatar

@tykus He is logged in though, or it wouldn't redirect to /home when visiting /login. It would show the login form if he wasn't logged in?

@sanjayacloud please post your entire /routes/web.php file.

sanjayacloud's avatar

@tykus I have properly authenticated and, I can access my user data I my /home page. I I cannot see button or for in my header.

sanjayacloud's avatar

@Cronix

here's my web.php

<?php



Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');

Route::resource('profiles', 'ProfileController');
Route::get('/profile','ProfileController@user');

tykus's avatar

Neither of the /home routes is protected to authenticated users!!!!

If you are logged in you would see the authenticated user whenever you dump(auth()->user()) - don't know how many more ways to say this...

// Login, Register Logout etc
Auth::routes();

Route::middleware('auth')->group(function () {
    Route::get('/home', 'HomeController@index')->name('home');

    Route::resource('profiles', 'ProfileController');
    Route::get('/profile','ProfileController@user');
});
Cronix's avatar

change to

<?php

Route::get('/', function () {
    return view('welcome');
});

Route::get('/home', 'HomeController@index')->name('home');
Route::get('/profile','ProfileController@user');
Route::resource('profiles', 'ProfileController');

Auth::routes();
sanjayacloud's avatar

@tykus I think Yes. If not covered by the session, How can i do this. I use below class in ProfileController

use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;


tykus's avatar

Routes protected by authentication middleware should be within the auth middleware group as I show above.

Have you changed anything in app/Http/Kernel.php file?

tykus's avatar

@tykus not using the auth middleware shouldn't prevent you from viewing the current logged in user though, only prevent them from accessing the route if they aren't authenticated.

I never ever said it was a requirement @Cronix - if you thought about it for a minute, why would you have a profile route that is accessible to a non-authenticated user.

munazzil's avatar

@sanjayacloud

Use as like below

  public function user(User $user,$id)

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

Please or to participate in this conversation.