Courses

Multi-Language Laravel 11: All You Need to Know

Language Switcher with Sessions and DB

You're reading a FREE PREVIEW of a PREMIUM course.
Summary of this lesson:
- Add language field to users table
- Create ChangeLanguageController for language switching
- Implement SetLocale middleware for user/guest language preferences
- Store language preference in database for logged-in users
- Use session for guest language selection

Link to the repository

[Only for premium members]

Another example of how we can add a language selector to our application is by using sessions and a DB table to store the user's preferred language.

This example will not use a URL to determine the language which will allow us to use the same URL for all languages.


Setup

Our setup process for Database stored language selection will touch these things:

  • Configuration: Adding a language list to our configuration - this will be used to display the language selector
  • DB Structure: Creating a new field on the users table called language
  • Controller: Create a controller that will handle the language change
  • Middleware: Add a middleware to handle language settings based on user's preferences
  • Views: Adding a language selector to our views
  • Routes: Adding a route that will allow us to change the language

Let's start with the configuration:...

The full lesson is only for Premium Members.
Want to access all 18 text lessons of this course? (80 min read)

You also get:

  • 78 courses
  • Premium tutorials
  • Access to repositories
  • Private Discord