DEV Community

Cover image for Shortcut/Shorten To Get Authenticated User ID in Laravel 8
Code And Deploy
Code And Deploy

Posted on • Edited on

3 2

Shortcut/Shorten To Get Authenticated User ID in Laravel 8

Originally posted @ https://codeanddeploy.com visit and download the sample code: https://codeanddeploy.com/blog/laravel/shortcutshorten-to-get-authenticated-user-id-in-laravel-8

Advanced Laravel SAAS Starter Kit with CRUD Generator

Advanced Laravel SAAS Starter Kit with CRUD Generator - GET YOUR COPY NOW!

In this post, I will share a simple example of how to get the user ID of the authenticated user in Laravel 8. Using the auth() helper function or Auth class in Laravel 8 you will easily get the user ID.

The question is how?

First, we know that auth()->user() or \Auth::user() we can access the authenticated user details from the users table.

So getting the user ID you can access it with the sample below:

echo auth()->user()->id;

//or

echo \Auth::user()->id;

Enter fullscreen mode Exit fullscreen mode

But we can shorten it by removing the user() function. Now it will become like this:

echo auth()->id();

//or

echo \Auth::id();
Enter fullscreen mode Exit fullscreen mode

Now the result of the code above is the same but the difference is your code is shorter.

Advanced Laravel SAAS Starter Kit with CRUD Generator

Advanced Laravel SAAS Starter Kit with CRUD Generator - GET YOUR COPY NOW!

I hope this tutorial can help you. Kindly visit here https://codeanddeploy.com/blog/laravel/shortcutshorten-to-get-authenticated-user-id-in-laravel-8 if you want to download this code.

Happy coding :)

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️