How to get logged in user data into Laravel controller
Asked Answered
G

10

10

I am working on laravel 5.4, i have used the auth for user login at client side,now i want the logged in user details at the Controller,

view side by writing below code i got that:

{{ Auth::user()->name }} // this works on view page only.

Suggest me the library files with the code. I want to display some user data like name,age,dob,etc after logged in.

Gyve answered 22/7, 2017 at 18:41 Comment(1)
Jeez, did you at least check the documentation before asking that question?Hydrotropism
R
30

The laravel Auth Facade is used to get the autheticated user data as

$user = Auth::user();
print_r($user);

This will work in your controller and view both, but you have to include it as

use Illuminate\Support\Facades\Auth;
Renfrow answered 22/7, 2017 at 18:46 Comment(0)
G
9

Just use the helper function you won't need to instantiate or import any class.

$user = auth()->user();

then dd($user); you'll have a full data on user.

you can then pull what you want.

$user->name

etc...

Gmur answered 23/7, 2017 at 4:18 Comment(0)
H
3

This should work

use Illuminate\Support\Facades\Auth;

// Get the currently authenticated user...
$user = Auth::user();

But you have to use use

Hamm answered 22/7, 2017 at 18:44 Comment(1)
Thank you for your help.Gyve
S
2

Laravel has helpler for that. u can use auth() anywhere. for example:

auth()->user()->name

or check if not authentificated:

if(! auth()->user()){}
Sibylle answered 22/7, 2017 at 18:47 Comment(0)
H
2

You can access the user in any controller using

$user = Auth::user();

You should then be able to get details of the user by doing things like

$user_id = $user->id; //or Auth::user()->id;
$user_email = $user->email; // or Auth::user()->email;

See more details here https://laravel.com/docs/5.4/authentication#retrieving-the-authenticated-user

Honkytonk answered 22/7, 2017 at 18:47 Comment(0)
E
2

You can use auth()->user->name

Ecumenicalism answered 23/7, 2017 at 4:31 Comment(0)
L
2

Use the facade

use Illuminate\Support\Facades\Auth;

And

$user = Auth::user();

NOTE: You can call specific column in this library like $user = Auth::user()->name; or ->address etc.

Lobito answered 28/10, 2019 at 21:12 Comment(0)
T
0

$user = Auth::user();

You could get the current user into the user variable.

Thanatos answered 11/8, 2020 at 9:28 Comment(0)
W
0

For authentication purpose we can use middleware in controller given as below

public function __construct()
{
    $this->middleware('auth');
    
}

Then write in your function $user = Auth::user();

Woden answered 22/3, 2023 at 13:8 Comment(0)
G
0

This worked, but on blade.php :

@auth
    <form action="/registration" method="post" enctype=multipart/form-data>
        @csrf
      <div class="form-floating">
        <input id="email" name="email" type="email" placeholder="Email" value="{{ auth()->user()->email }}" readonly>
      </div>
    </form>
@endauth
Grillo answered 4/9, 2023 at 6:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.