Vigyata.AI
Is this your channel?

3 Different Ways To Get Authenticated User In Laravel

15.0K views· 226 likes· 4:07· Jun 2, 2023

🛍️ Products Mentioned (3)

Here, we will be looking at 3 different ways to get authenticated users in Laravel. === OFFER === GET 30% DISCOUNT ON ALL PLANS OF CLOUDWAYS HOSTING FOR 3 MONTHS https://unified.cloudways.com/signup?id=1029148&coupon=CW30FOR3 === Important Links === Fonts, extensions I use, and Support Laratips links: https://bit.ly/m/laratips === Video Related Links === === Other Videos === What's New In Laravel 9 https://youtu.be/qAzrZUG_9Z0 New In Laravel Playlist https://www.youtube.com/playlist?list=PL2DahmvUpeuu4UVWrcxlvVkMiI0SeS2OZ Laravel Package Development Playlist https://www.youtube.com/playlist?list=PL2DahmvUpeuu8HkpbNT-cKernolVdqZq_ Password Validation In Laravel with at least one Letters, Numbers, Upper and Lowercase or Symbols: https://youtu.be/f-g3P9imPZs Multiple Apis With ForwardsCalls Trait And Code Refactoring Tip - Laravel Hidden Feature: https://youtu.be/vzgNm86ezWE Laravel Chunking - You Might Be Doing This Wrong - Problem and Solution: https://youtu.be/aQCHmB4Uh0Q Laravel Tip - Properly Get Data Between Two Dates https://youtu.be/bJkEBTg1NGA Get Data From Multiple Databases In One Project | Laravel Tip https://youtu.be/KJAXLf78QPs === Attribution === Boy waving hand Video by VideoPlasty .com (https://pixabay.com/users/videoplasty-19751245/?utm_source=link-attribution&utm_medium=referral&utm_campaign=video&utm_content=80696) from Pixabay #laravel #php #laraveltip #laratips

About This Video

Hey guys, welcome to Laratips. In this video I’m showing you 3 different ways to get the authenticated user in a Laravel app, because one of the developers who just joined my office asked me this exact thing. The first two ways are the common ones: using the auth() helper and calling auth()->user(), and then using the Auth facade (Illuminate\Support\Facades\Auth) and calling Auth::user(). Both will return the currently authenticated user, so if you’re logged in (like I was as admin), you’ll see that user coming back. But here’s the real reason I made this video: with those two approaches, you don’t always get the best developer experience when you want to call your custom methods on the User model (like a short_name accessor/method). So I show you the third option I personally use: creating an app/helpers.php file with an auth_user() helper that returns User|null. Then you register that file in composer.json under autoload -> files, run composer dump-autoload, and now you get proper autocomplete for your User model methods. It’s a small setup, but it makes day-to-day Laravel work cleaner and faster.

Frequently Asked Questions

🎬 More from Laratips