You might have seen this issue in laravel 8 and above versions. Its very obvious from the error message that data is not available which we trying to access in navigation.blade.php file.
This issue happens all of the sudden navigating within the websites. The reason behind this issue is that the user session is expired and laravel needs a user to login again.
There are few solutions through which we can fix this problem.
- .env file
- config/session.php
- routes/web.php
.env file
In .env file, you can increase default session lifetime from 120 seconds to 1200 or 12000 seconds.
SESSION_LIFETIME=12000
You will need to run following command after making these changes
php artisan config:clear
config/session.php
Inside config directory you will find sessions.php file which has setting configuration for sessions environment variables. Here you can increase the life time of session as we did in .env file.
?php
use Illuminate\Support\Str;
return [
/*
|--------------------------------------------------------------------------
| Default Session Driver
|--------------------------------------------------------------------------
|
| This option controls the default session "driver" that will be used on
| requests. By default, we will use the lightweight native driver but
| you may specify any of the other wonderful drivers provided here.
|
| Supported: "file", "cookie", "database", "apc",
| "memcached", "redis", "dynamodb", "array"
|
*/
'driver' => env('SESSION_DRIVER', 'file'),
/*
|--------------------------------------------------------------------------
| Session Lifetime
|--------------------------------------------------------------------------
|
| Here you may specify the number of minutes that you wish the session
| to be allowed to remain idle before it expires. If you want them
| to immediately expire on the browser closing, set that option.
|
*/
'lifetime' => env('SESSION_LIFETIME', 12000),
You will need to run following command after making these changes
php artisan config:clear
routes/web.php
We can force a group of routes or single routes to check for user session, if session is expired, redirect user to login page. To do so, we need to call a middleware auth on particular route or group of routes.
Route::get('/dashboard', [HomeController::class, "index"])->middleware(['auth','verified'])->name('dashboard');