Source: paulund.co.uk

Laravel Cache Auth::user()
If you use https://github.com/barryvdh/laravel-debugbar when developing your app you'll notice there is a tab that will show you all the database queries your application is making. This is a great tool to spot areas where you can improve the database queries or if there's data you can cache instead of making another database call. When working on a project I noticed that when logged in, the application was making at least one extra database query to fetch the logged in user.

These two calls will look up the user ID in session and then go an fetch the user record from the database based off this ID, therefore this is something we can change to cache the user record.

When fetching users Laravel will use our new provider where we can lookup in cache first instead of fetching from the database.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners