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

Posted 3 years ago by ashleywnj

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();

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();
    return view('reviews/adr', compact('user'));

Thanks for any guidance.

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.