10

I'm working with Laravel and Databases (DB) using Eloquent and MySQL.

I already found the code to close the DB connection, but I was searching on the Documentation if Laravel close all the connections automatically after any use of it.

Before using Laravel, every time I used a MySQL command I opened the database connection on the page header and closed it on the page footer.

But, what about using Laravel/Eloquent? Does it close? Or do I need to close manually all the connections after using it?

Like, basic example using Laravel/Eloquent:

$user = new User;
$user->name = 'John';
$user->save();

I know that on the beginning Laravel opens a connection to the Database/User table. But after saving, does it close it?

saulob
  • 615
  • 1
  • 10
  • 25
  • 1
    MySQL will close them automagically after a while, don't worry about it too much. – Andrei P. Mar 02 '15 at 13:44
  • 1
    It will close the connections on termination of the script, no different to any other PHP script that doesn't close database connections explicitly – Mark Baker Mar 02 '15 at 13:44
  • possible duplicate of [Is it necessary to close PDO connections](http://stackoverflow.com/questions/15444748/is-it-necessary-to-close-pdo-connections) – Wader Mar 02 '15 at 16:27

1 Answers1

14

Yes.

With PHP and with Laravel connections are automatically closed at the end of the script.

lud
  • 1,941
  • 20
  • 35
  • 4
    @SameerShaikh Maybe you want to monitor connections : http://stackoverflow.com/questions/2453308/monitoring-used-connections-on-mysql-to-debug-too-many-connections – lud Sep 30 '15 at 07:37
  • what do you mean by the end of the script? So for example, if an eloquent model returned the data it will close the connection to the DB automatically? Or the RAW queries. – Fariman Kashani Aug 16 '21 at 17:08
  • 1
    @FarimanKashani I mean when the PHP execution is finished. If it starts on index.php then when the execution reaches the end of index.php. – lud Aug 17 '21 at 13:41