PHPackages                             jeremy379/laravel-disconnectable - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. jeremy379/laravel-disconnectable

ActiveLibrary[Database &amp; ORM](/categories/database)

jeremy379/laravel-disconnectable
================================

Laravel Eloquent trait to disconnect models from the database at runtime

0.0.1(8mo ago)00MITPHPPHP &gt;=8.2

Since Sep 4Pushed 8mo agoCompare

[ Source](https://github.com/jeremy379/laravel-disconnectable)[ Packagist](https://packagist.org/packages/jeremy379/laravel-disconnectable)[ RSS](/packages/jeremy379-laravel-disconnectable/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (3)Versions (2)Used By (0)

Eloquent Disconnectable
=======================

[](#eloquent-disconnectable)

**Disconnect Laravel Eloquent models from their database connection at runtime.**

---

Why this package?
-----------------

[](#why-this-package)

In many applications following **Clean Architecture** or **Hexagonal Architecture**, you want to:

- **Prevent database connections from leaking** into the Domain or Application layers.
- **Ensure models returned from the infrastructure layer are read-only** and safe to use.
- **Avoid accidental lazy-loading or persisting** in views, services, or domain objects.

`eloquent-disconnectable` provides a simple way to “cut” the database connection of a model or collection before returning it from repositories or infrastructure services, while keeping the original model attributes and already loaded relations accessible.

---

### Benefits

[](#benefits)

- ✅ **Prevents lazy-loading** of relations.
- ✅ **Prevents persistence operations** (`save`, `delete`, `push`) on disconnected models.
- ✅ Keeps **already loaded relations** accessible.
- ✅ Helps enforce **Clean Architecture principles** by isolating the domain from database concerns.

---

Installation
------------

[](#installation)

Require via Composer:

```
composer require jeremy379/eloquent-disconnectable
```

---

Usage
-----

[](#usage)

### In a Model

[](#in-a-model)

```
use Illuminate\Database\Eloquent\Model;
use YourVendor\EloquentDisconnectable\Disconnectable;

class User extends Model
{
    use Disconnectable;
}
```

---

### Disconnect a single model

[](#disconnect-a-single-model)

```
$transaction = User::with('order')->first();
$disconnected = $transaction->disconnect();

// Access normal attributes
echo $disconnected->id;

// Access already loaded relations
echo $disconnected->order->id;

// Attempting DB operations will fail
$disconnected->save();       // RuntimeException
$disconnected->load('cart'); // RuntimeException
$disconnected->otherRelation; // RuntimeException
```

---

### Why and where to use it

[](#why-and-where-to-use-it)

- **Infrastructure layer / repository:** disconnect models before returning them to the Domain layer.
- **Domain layer / services / controllers:** ensures models are **read-only** and **cannot accidentally hit the database**.
- **View / API layer:** safe to render attributes and loaded relations without DB risk.

---

### Clean Architecture Enforcement

[](#clean-architecture-enforcement)

```
+-------------------+
| Domain Layer      |
| (Entities, DTOs)  |
| - No DB access    |
+-------------------+
          ↑
          |
+-------------------+
| Infrastructure    |
| (Repositories)    |
| - DB access only  |
+-------------------+

```

By disconnecting Eloquent models:

- Domain layer **never touches the DB**.
- Models are **pure data objects** for business logic.
- Already loaded relations are preserved, so performance stays optimal.

---

### License

[](#license)

MIT © Jérémy Dillenbourg

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance60

Regular maintenance activity

Popularity0

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity38

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

250d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2dd11563c30299f3c61e050728740580a889f0b286cd1c7c28f88acc0bcfc5f9?d=identicon)[jeremy379](/maintainers/jeremy379)

---

Top Contributors

[![jeremy379](https://avatars.githubusercontent.com/u/5048337?v=4)](https://github.com/jeremy379 "jeremy379 (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jeremy379-laravel-disconnectable/health.svg)

```
[![Health](https://phpackages.com/badges/jeremy379-laravel-disconnectable/health.svg)](https://phpackages.com/packages/jeremy379-laravel-disconnectable)
```

###  Alternatives

[doctrine/orm

Object-Relational-Mapper for PHP

10.2k285.3M6.2k](/packages/doctrine-orm)[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k115.1M102](/packages/jdorn-sql-formatter)[illuminate/database

The Illuminate Database package.

2.8k52.4M9.4k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M546](/packages/mongodb-mongodb)[ramsey/uuid-doctrine

Use ramsey/uuid as a Doctrine field type.

90340.3M211](/packages/ramsey-uuid-doctrine)[reliese/laravel

Reliese Components for Laravel Framework code generation.

1.7k3.4M16](/packages/reliese-laravel)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
