First, update your `.env` file and set the `SESSION_DOMAIN` variable to the base domain of your project. For example, if your main domain is `example.com`, set it as follows:
SESSION_DOMAIN=.example.com
Next, you need to update the `config/session.php` file. Set the `domain` option to `’.example.com’` so that the session cookie is available across all subdomains:
'domain' => '.example.com',
After making these changes, be sure to clear your Laravel application cache:
php artisan cache:clear
Now you should be able to share sessions between subdomains. For example, if you have two subdomains `sub1.example.com` and `sub2.example.com`, the session data will be accessible on both subdomains.
In your code, you can access the shared session data using the standard Laravel session methods like `session(‘key’)` or `request()->session()->get(‘key’)`.
Here is an example of how you can set a session variable:
session(['key' => 'value']);
And here is an example of how you can retrieve the session variable:
$value = session('key');
Remember to replace `’key’` and `’value’` with your own key-value pairs.
That’s it! You should now have a better understanding of how to share sessions between subdomains in Laravel.