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

ashleywnj's avatar

Auth::user() versus Auth::User->select('id', 'name')->get()

In my view I want to capture the ID and name of the user currently signed in - but I don't need all the other fields - so I figured I would strip out only those data fields I need before passing to the view.

I can get all the data related to the user signed in if I use

$user = Auth::user(); 

Or if I use

    if (Auth::user()) {
        $user = User::all();
        dd($user);
    }

However if I attempt to capture just the fields I need then it actually returns every use registered in the user table - this is what I attempted - but it pulls back those elements for each user in the table - is this by design and just my lack of knowledge of how php behaves ?

  public function index()
    {
    $user = Auth::user()->select('id', 'name')->get();
    dd($user);
    return view('reviews/adr', compact('user'));
    }

Thanks for any guidance.

0 likes
3 replies
StormShadow's avatar
Level 51

User::all() returns all of the rows in your user table. If you want the details of the current user

$user = Auth::user():
 $id = $user->id;
 $name = $user->name;
 dd($user); //dump the Eloquent User object. 

Pretty sure the user gets cached per request by Laravel, any performance or memory difference by getting only the columns you need would be extremely small IMHO. Just grab the user and use only the fields you need. The db is hit anyway at least once to get the user, you'll get all fields for the row. Haven't seen the select thing you are doing off of the User object anywhere, but as you say it's not doing what you want, my guess is as there is no where clause in it, so it's getting all rows. My $0.02

1 like
tedeumf's avatar

Hi, please help, I am new to laravel, and i don't know where even to start, or if i am asking the question correct...

I have two tables: "students" and "users". I use students for all students data. Now i want to allow these students to have access, so i use laravel auth to make users. I want when they login to have access only for records matching theyr email address. How can i do this to load a page where $students id is equal to user that have the same id as students?

I don't know where even to start. Thank you so much! ''' I tryed nothing as i don't know where to start. '''

ollie_123's avatar

Hi @tedeumf

Whilst the subject is similar, you should really start your own thread with that question as this thread has been solved & closed. However for your question, if you include a user_id on the students table you can then call the required data where the user_id matches the authorised user. Something like

$studentData = \App\Models\Student::where('user_id', Auth::user()->id)->get() 
//or ->first() depending on what you're looking to do.

Please or to participate in this conversation.