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

eXist73's avatar

Does PHP Artisan Tinker maintain an active DB Connection?

As the title describes, I'm curious exactly how a terminal session with php artisan tinker behaves.

The main question being - Does php artisan tinker maintain an ACTIVE DB Connection while its open?

For example, if we have a DB connection timeout of 10 minutes, will that php artisan tinker session do if we try to execute a new SQL command after 10 minutes?

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

Yes, php artisan tinker does maintain an active database connection while it's open. However, it's important to note that the underlying database management system (DBMS) may have its own timeout settings that could cause the connection to close if it's idle for too long.

When you start a tinker session, Laravel establishes a database connection based on your application's configuration settings. This connection will remain active as long as the tinker session is running. If you try to execute a new SQL command after a period of inactivity, one of two things could happen:

  1. If the DBMS has not closed the connection due to its own timeout settings, the command will execute as expected.
  2. If the DBMS has closed the connection due to inactivity, Laravel's database connection manager should automatically attempt to reconnect when you try to run a new command.

Here's an example of how you might see this behavior in a tinker session:

// After starting tinker, you can run database queries like so:
>>> App\Models\User::first();
// This will fetch the first user from the database.

// If you wait for more than the DB connection timeout and then try to run another query:
>>> App\Models\User::find(2);
// One of two things will happen:
// - The query will run successfully if the connection is still open.
// - If the connection was closed due to timeout, Laravel should try to reconnect and then run the query.

If you find that the connection is not being re-established automatically after a timeout, you may need to look into your database configuration settings or consider implementing a custom solution to handle reconnection attempts.

1 like

Please or to participate in this conversation.