Skip to content

Commit b896dbd

Browse files
author
Mehmet Nurullah Sağlam
committed
added override connection attribute in models tip
1 parent e3fca51 commit b896dbd

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

db-models-and-eloquent.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
- [New scalar() method](#new-scalar-method)
100100
- [Select specific columns](#select-specific-columns)
101101
- [Chain conditional clauses to the query without writing if-else statements](#chain-conditional-clauses-to-the-query-without-writing-if-else-statements)
102+
- [Override Connection Attribute in Models](#override-connection-attribute-in-models)
102103

103104
### Reuse or clone query()
104105

@@ -2118,3 +2119,37 @@ class RatingSorter extends Sorter
21182119

21192120
Tip given by [@mmartin_joo](https://twitter.com/mmartin_joo/status/1521461317940350976)
21202121

2122+
### Override Connection Attribute in Models
2123+
2124+
Overriding the database connection attribute for individual models in Laravel can be a powerful technique. Here are a few use cases where you might find it especially handy:
2125+
2126+
#### 1. Multiple Database Connections
2127+
2128+
If your application uses multiple database connections (e.g., MySQL, PostgreSQL, or different instances of the same database), you may want to specify which connection should be used for a particular model. By overriding the `$connection` property, you can easily manage these connections and ensure your models are interacting with the appropriate databases.
2129+
2130+
#### 2. Data Sharding
2131+
2132+
In cases where you're using data sharding to distribute your data across multiple databases, you might have different models that map to different shards. Overriding the connection attribute in each model allows you to define which shard should be used without affecting other models or the default connection.
2133+
2134+
#### 3. Third-Party Integration
2135+
2136+
When integrating with third-party services that provide their own database, you may need to use a specific connection for a model representing data from that service. Overriding the connection attribute in that model will ensure it connects to the right database while keeping your application's default settings intact.
2137+
2138+
#### 4. Multi-Tenancy Applications
2139+
2140+
In a multi-tenant application, you may have separate databases for each tenant. By overriding the `$connection` property dynamically in your models, you can easily switch between tenant databases based on the current user, ensuring data isolation and proper resource management.
2141+
2142+
To override the connection attribute in a model, define the `$connection` property within the class:
2143+
2144+
```php
2145+
<?php
2146+
2147+
namespace App\Models;
2148+
2149+
use Illuminate\Database\Eloquent\Model;
2150+
2151+
class CustomModel extends Model
2152+
{
2153+
protected $connection = 'your_custom_connection';
2154+
}
2155+
```

0 commit comments

Comments
 (0)