The "Connection Established" message in your debug bar indicates that a new database connection was initiated. This typically happens when you switch between different database connections within the same request. In your case, you have two connections defined in your database.php configuration: mysql and mysql_with_prefix. When you use models that are associated with different connections, Laravel will establish a new connection for each one as needed.
Here's a breakdown of what's happening:
-
Connection Switching: When you use a model that specifies a different connection (
protected $connection = 'mysql_with_prefix'), Laravel will establish a new connection to the database using the specified configuration. This is necessary because each connection can have different settings, such as prefixes, host, database, etc. -
Performance Impact: Establishing a new connection can have a slight performance impact, especially if it happens frequently within a single request. However, in most cases, this impact is minimal unless you are making a large number of connection switches.
-
Optimization Tips:
- Minimize Connection Switching: Try to minimize the number of times you switch connections within a single request. Group operations that use the same connection together if possible.
- Connection Pooling: Ensure that your database server is configured to handle multiple connections efficiently. Connection pooling can help mitigate the overhead of establishing new connections.
-
Debugging: The debug bar is useful for identifying when new connections are established, which can help you optimize your application's database interactions.
In summary, the "Connection Established" message is a normal part of using multiple database connections in Laravel. While it does indicate a new connection, the performance impact is usually minor unless excessive connection switching occurs.