0

In my laravel project, when i want to get the current user ID, i use this logic bellow:

public function showuserid() {

    $userid = Auth::id(); //this is the simplest way i found of how to get the current user's id

    return view('perfil/mostrarid')->with('mostrarid',  $userid);

}

My problem is, my table 'users' in my database, has an extra column called 'credencial'.

I tried to do $userid = Auth::credencial(); but it doesn't work and i get an error:

Method Illuminate\Auth\SessionGuard::credencial does not exist.

Can i get that value using Auth somehow? Is there any other way, as simple as Auth, to get the value?

6
  • 3
    Try Auth::user()->credencial Commented Feb 21, 2020 at 20:10
  • Auth::user()->credencial doesn't work. I get this following error: "Trying to get property 'credencial' of non-object" Commented Feb 21, 2020 at 20:14
  • But is an autethicated user wich send the request? Auth::id() will not work also if there is not an authenticated user laravel.com/docs/5.8/… Commented Feb 21, 2020 at 20:20
  • Auth::id() works because the user is authenticated. Auth::credencial() does not work because 'credencial' is a custom column that i put in 'users' table. Commented Feb 21, 2020 at 20:36
  • 2
    Auth::credencial() deosn't works because credencial it's not an Auth method. What I suggest was this Auth::user()->credencial. And what I mean it's if Auth::user() reurns null, Auth::id() will return null too. If it's there an authenticated user , Auth::user() will return the user table fields Commented Feb 21, 2020 at 20:45

1 Answer 1

1

My problem is, my table 'users' in my database, has an extra column called 'credencial'.

You need to access a logged-in user's property. To do so, try this:

$credential = Auth::user()->credential;

Now, in order to avoid to throw the error:

Trying to get property 'credencial' of non-object

Make sure that your user is logged-in. You could do this checking it first:

if (Auth::check())
{
    $credential = Auth::user()->credential;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I don't know what happened. Before i posted the question, i tried Auth::user()->credential and it wasn't working. Now that i checked again, seems like Auth::user()->credential works fine. I tested with the user logged in all the time. Thanks anyway.
There was surely a typo in your code. Auth::user() gives you access to the user's field in the database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.