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?
Most CMSs close the MySQL connection at the end of the request, which is really meaningless, because PHP will do it anyway.
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.
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.
Yes.
With PHP and with Laravel connections are automatically closed at the end of the script.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With