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

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners