I am a bit confused about laravel database connection.
If I execute a laravel code that select from database three queries as example.
If laravel establish database connection and login into database then logout for each executing query or laravel connect with the first query and logout after the last query.
// login to the database
User::all();
// logout from database
// login to the database
Info::all();
// logout from database
// login to the database
Family::all();
// logout from database
or
// login to the database
User::all();
Info::all();
Family::all();
// logout from database
I am asking that for database session not on the code level.
All regards
2
Answers
Typically, Laravel will establish a DB connection once and reuse that connection for subsequent queries within the same request.
In other words, Laravel keeps the database connection open within the same request.
It doesn’t log in and out for each query.
Laravel will establish a single database connection and execute all the queries
Once all the queries are executed, Laravel will close the connection. Laravel tries to reuse connections as much as possible to reduce overhead. Laravel doesn’t open and close the database connection for each query by default.
However, generally, you don’t need to worry about managing connections explicitly in Laravel as it handles connection pooling and management internally for you.