skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. Laravel will establish a single database connection and execute all the queries

    (User::all(), Info::all(), Family::all())
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search