Skip to content

Commit aa8a4a0

Browse files
authored
Merge pull request #128 from mnurullahsaglam/master
add override connection attribute in models
2 parents e3fca51 + 9bc9061 commit aa8a4a0

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ Or if you want the Chinese version:
2222
[中文版本](https://github.com/Lysice/laravel-tips-chinese/blob/master/README-zh.md)
2323

2424
---
25-
**Update 19 April 2023**: Currently there are **353 tips** divided into 14 sections.
25+
**Update 24 April 2023**: Currently there are **354 tips** divided into 14 sections.
2626

2727
## Table of contents
2828

29-
- [DB Models and Eloquent](db-models-and-eloquent.md) (97 tips)
29+
- [DB Models and Eloquent](db-models-and-eloquent.md) (98 tips)
3030
- [Models Relations](models-relations.md) (36 tips)
3131
- [Migrations](migrations.md) (15 tips)
3232
- [Views](views.md) (20 tips)

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)