This tutorial will cover possible fixes for the issue highlighted above, i.e. ‘Trying to get property ‘name’ of non-object navigation.blade.php'
. We mostly encounter these types of errors in Laravel 8 and above versions. Pretty clear, the message shows the root cause, that data is not found that we are trying to access in the navigation.blade.php
file.
This error can happen while navigating within websites. The core reason behind this error is when a user session gets expired and Laravel requires the user to log in again.
We will discuss a few solutions to fix this problem.
.env file
In .env file, you can raise the default session lifetime from 180 seconds to 1800 or 18000 seconds.
SESSION_LIFETIME=18000
You have to run the following command after making your changes
php artisan config:clear
config/session.php
In the config directory of your Laravel project, you will find the sessions.php
file, that has a setting configuration for ‘session environment variables’. In this file, we can increase the life time of a session as we did in the .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', 18000),
Again, run the following command after making your changes.
php artisan config:clear
routes/web.php
You can force a single or group of routes to check for user session. Suppose a session gets expired, redirect the user to login page. To do this, you will need to call a middleware auth on particular route or group of routes as shown below.
Route::get('/dashboard', [HomeController::class, "index"])->middleware(['auth','verified'])->name('dashboard');