Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Laravel close automatically the DB connections?

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?

like image 478
saulob Avatar asked Mar 02 '15 13:03

saulob


People also ask

Does php automatically close MySQL connection?

Most CMSs close the MySQL connection at the end of the request, which is really meaningless, because PHP will do it anyway.

Should I close DB connection php?

For the purpose of safe coding, you should always close database connections explicitly to make sure that the code was able to close itself gracefully and to prevent any other objects from reusing the same connection after you are done with it.

How do I know if DB is Laravel connected?

Echo the Laravel database name in Blade/PHP The simplest way it to place the following script in a Blade or PHP file. This will output the name of the database or return 'none' if there is no connection. If you view it in the browser, it gives you the name of the connected database.


1 Answers

Yes.

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

like image 177
lud Avatar answered Oct 18 '22 16:10

lud