PHPackages                             lexbelcode/laravel-eloquent-dynamic-tables-for-models - 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. lexbelcode/laravel-eloquent-dynamic-tables-for-models

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

lexbelcode/laravel-eloquent-dynamic-tables-for-models
=====================================================

A simple but effective way to set the table of a model at runtime and continue using Eloquent instead of QueryBuilder

v0.1.0(2y ago)55MITPHPPHP ^8.1

Since Nov 28Pushed 2y ago1 watchersCompare

[ Source](https://github.com/LexBel-co/laravel-eloquent-dynamic-tables-for-models)[ Packagist](https://packagist.org/packages/lexbelcode/laravel-eloquent-dynamic-tables-for-models)[ RSS](/packages/lexbelcode-laravel-eloquent-dynamic-tables-for-models/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (3)Used By (0)

Laravel Eloquent Dynamic Tables for Models
==========================================

[](#laravel-eloquent-dynamic-tables-for-models)

\###A simple but effective way to set the table of a model at runtime and continue using Eloquent instead of QueryBuilder

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

[](#installation)

The package can be installed via Composer:

`composer require lexbelcode/laravel-eloquent-dynamic-tables-for-models`

Description
-----------

[](#description)

Consider this scenario:

You have, for any razon, a database in which exists A) tables with different names but same structure, or you need break a really big table into smallers and you don't want to use the partitioning provided by your database, and B) tables with different names but same structure that has a relationship with the tables in A)

Example: In general, if you have one model for sells and one model for buyers, and you need to get records from the sells table with the realted data from buyers, using Eloquent you will do this:

```
$data = Sell::select(...here your columns...)
    ->where(...here your filters...)
    ->with('buyers')
    ->get();
```

But if the scenario changes and you have to divide your tables into multiples like this: sells\_01 sells\_02 sells\_03 sells\_04

buyers\_01 buyers\_02 buyers\_03 buyers\_04

Your models are the same, but if you need to get data from sells\_03 with the realted data from buyers\_03 **you can't use Eloquent like before**, because your Sell model is pointed to the sells table and your Buyer model is pointed to the buyers table, so, the only way to get the data is through the QueryBuilder or raw querys using joins, and you will lost the magic of Eloquent, inclusive, if you have Eloquent events, you won't be able to fire them because of this

But if you update your models, to extend from the LaravelEloquentDynamicModel class, you can do it as simple as this:

Your models:

```
use lexbelcode\LaravelEloquentDynamicTablesForModels\LaravelEloquentDynamicModel;

class Sell extends LaravelEloquentDynamicModel
{
    ...
}
```

```
use lexbelcode\LaravelEloquentDynamicTablesForModels\LaravelEloquentDynamicModel;

class Buyer extends LaravelEloquentDynamicModel
{
    ...
}
```

And in your code, you only have to add this two lines of code:

```
Sell::$dynamicTableName = 'sells_03';
Buyer::$dynamicTableName = 'buyers_03';

$data = Sell::select(...here your columns...)
    ->where(...here your filters...)
    ->with('buyers')
    ->get();
```

And during the time execution of the related PHP script, model Sell will be pointing to sells\_03 table and model Buyer will be pointing to buyers\_03 table, and you'll can use Eloquent normaly with that models, relationships, events, etc.

Requirements
------------

[](#requirements)

- **[PHP 8.1+](https://php.net/)**
- **[Laravel 10+](https://laravel.com)**

Author
------

[](#author)

Alex Beltrán Follow me on X as [@LexBelCode](https://twitter.com/LexBelCode)

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity42

Maturing project, gaining track record

 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

896d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/db44d5a9c1ee6d0421336783d69f52be49a3e26b33bc4d4430a4eaab8ec1cc9f?d=identicon)[lexbelcode](/maintainers/lexbelcode)

---

Top Contributors

[![LexBel-co](https://avatars.githubusercontent.com/u/14348360?v=4)](https://github.com/LexBel-co "LexBel-co (1 commits)")

---

Tags

eloquenteloquent-molaravellaravel-packagephp

### Embed Badge

![Health badge](/badges/lexbelcode-laravel-eloquent-dynamic-tables-for-models/health.svg)

```
[![Health](https://phpackages.com/badges/lexbelcode-laravel-eloquent-dynamic-tables-for-models/health.svg)](https://phpackages.com/packages/lexbelcode-laravel-eloquent-dynamic-tables-for-models)
```

###  Alternatives

[anourvalar/eloquent-serialize

Laravel Query Builder (Eloquent) serialization

11320.2M21](/packages/anourvalar-eloquent-serialize)[overtrue/laravel-versionable

Make Laravel model versionable.

585308.0k5](/packages/overtrue-laravel-versionable)[abbasudo/laravel-purity

elegant way to add filter and sort in laravel

514330.5k1](/packages/abbasudo-laravel-purity)[statamic-rad-pack/runway

Eloquently manage your database models in Statamic.

135192.6k5](/packages/statamic-rad-pack-runway)[dragon-code/laravel-deploy-operations

Performing any actions during the deployment process

240173.5k2](/packages/dragon-code-laravel-deploy-operations)[stayallive/laravel-eloquent-observable

Register Eloquent model event listeners just-in-time directly from the model.

2928.9k7](/packages/stayallive-laravel-eloquent-observable)

PHPackages © 2026

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