Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rickbolton's avatar

Override config\session.php

I am wanting the lifetime minutes to be set within an admin settings and so I would like this value to be retrieved from a database rather than currently set within session.php

Is this possible to override anywhere or to at least point lifetime to a DB select?

0 likes
6 replies
moharrum's avatar

You can store the value of the life time in a table and retrieve it using helpers in the config 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' => get_life_time_form_db(),

The same way session driver is set form the .env file:

'driver' => env('SESSION_DRIVER', 'file'),
rickbolton's avatar

I'm currently trying to get the value using a model but that errors?

 App\Model\MainSetting::where('name', 'session_expiry')->first()

Do I just need to do a \DB:: instead?

moharrum's avatar

No need to use DB, by using first() you are returning a Model , instead try:

App\Model\MainSetting::where('name', 'session_expiry')->first()->session_lifetime // Assuming this is the field name

If that does not work, post here the result of:

dd(App\Model\MainSetting::where('name', 'session_expiry')->first());
rickbolton's avatar

I get Fatal error: Call to a member function connection() on null in /home/dev1/sites/xxx/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php

moharrum's avatar

Sorry @rickbolton , seems like this is not going to work, take a look at this stackoverflow issue, I guess we have to figure a way to query our setting without using Eloquent

rickbolton's avatar

Where is session.php config returned to? I saw this stackoverflow post and it does seem like we are trying to use DB or eloquent before it's initialized

Please or to participate in this conversation.