PHPackages                             laracraft-tech/laravel-dynamic-model - 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. laracraft-tech/laravel-dynamic-model

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

laracraft-tech/laravel-dynamic-model
====================================

Dynamic Model for Laravel!

v4.0.0(3mo ago)4512.4k↑28.1%3[2 issues](https://github.com/laracraft-tech/laravel-dynamic-model/issues)MITPHPPHP ^8.2CI passing

Since Sep 22Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/laracraft-tech/laravel-dynamic-model)[ Packagist](https://packagist.org/packages/laracraft-tech/laravel-dynamic-model)[ Docs](https://github.com/laracraft-tech/laravel-dynamic-model)[ RSS](/packages/laracraft-tech-laravel-dynamic-model/feed)WikiDiscussions main Synced today

READMEChangelog (10)Dependencies (21)Versions (14)Used By (0)

Dynamic Model for Laravel
=========================

[](#dynamic-model-for-laravel)

[![Latest Version on Packagist](https://camo.githubusercontent.com/b01b4bd266e2a15608a8b072194484a4ff332bc65d0c19cc9cc3a62bd1906e82/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c61726163726166742d746563682f6c61726176656c2d64796e616d69632d6d6f64656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/laracraft-tech/laravel-dynamic-model)[![Tests](https://github.com/laracraft-tech/laravel-dynamic-model/actions/workflows/run-tests.yml/badge.svg?branch=main)](https://github.com/laracraft-tech/laravel-dynamic-model/actions/workflows/run-tests.yml)[![Check & fix styling](https://github.com/laracraft-tech/laravel-dynamic-model/actions/workflows/fix-php-code-style-issues.yml/badge.svg?branch=main)](https://github.com/laracraft-tech/laravel-dynamic-model/actions/workflows/fix-php-code-style-issues.yml)[![License](https://camo.githubusercontent.com/93158fba347f9a467c9d477597b10f3f264015155aac978ccccfef726ffe58c4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6c61726163726166742d746563682f6c61726176656c2d64796e616d69632d6d6f64656c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/laracraft-tech/laravel-dynamic-model)[![Laravel Compatibility](https://camo.githubusercontent.com/52651fd5b5ccae3b8573847d6c34015c22a1011c9caaf403f8a8aa0b1acbbbc7/68747470733a2f2f62616467652e6c61726176656c2e636c6f75642f62616467652f6c61726163726166742d746563682f6c61726176656c2d64796e616d69632d6d6f64656c)](https://packagist.org/packages/laracraft-tech/laravel-dynamic-model)

Normally, each model in Laravel is written for only one table, and it's not so easy to break this convention. This is for a good reason - it ensures a well-designed and clean model. But in very specific cases, you may need to handle multiple tables via a single model. Here **Laravel Dynamic Model** comes into play! It provides you with an eloquent model which finally can handle multiple tables and if you want also multiple database connections!

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

[](#installation)

### Dependencies

[](#dependencies)

This package depends on Doctrine/DBAL, so make sure you have it installed.

```
composer require doctrine/dbal
```

### Package

[](#package)

```
composer require laracraft-tech/laravel-dynamic-model
```

Usage
-----

[](#usage)

### Let's create some dummy tables:

[](#lets-create-some-dummy-tables)

---

```
php artisan make:migration create_foo_table
php artisan make:migration create_bar_table
```

Create migrations for the tables:

```
Schema::create('foo', function (Blueprint $table) {
    $table->id();
    $table->string('col1');
    $table->integer('col2');
    $table->timestamps();
});

Schema::create('bar', function (Blueprint $table) {
    $table->date('period')->primary();
    $table->string('col1');
    $table->integer('col2');
    $table->timestamps();
});
```

```
php artisan migrate
```

### Let's create a Dynamic Model

[](#lets-create-a-dynamic-model)

---

If you want to create a **Dynamic Model** then you have to use the **DynamicModelFactory**. The Factory ensures, that the `table` and optionally the `connection` gets set for your new created model. Also it checks the schema of your provided table to set the propper values for: `primaryKey`, `keyType`, `incrementing`. Means, also if you defined your table schema to have a primary key called for instance **period** with a **date** type, the Factory will handle it for you.

Note that the default DynamicModel is set to **unguarded**. If you do not like this or you want your Dynamic Models have some custom functions, check the section below and create your own Dynamic Model.

```
use LaracraftTech\LaravelDynamicModel\DynamicModel;
use LaracraftTech\LaravelDynamicModel\DynamicModelFactory;

$foo = app(DynamicModelFactory::class)->create(DynamicModel::class, 'foo');

$foo->create([
    'col1' => 'asdf',
    'col2' => 123
]);

$faz = app(DynamicModelFactory::class)->create(DynamicModel::class, 'faz');

$faz->create([
    'period' => '2023-01-01',
    'col1' => 'asdf',
    'col2' => 123
]);

// optionally use another db connection (this one must be defined in your config/database.php file)
$fooOtherDB = app(DynamicModelFactory::class)->create(DynamicModel::class, 'foo', 'mysql2');
$fooOtherDB->create([...]);

dump($foo->first());
dump($faz->first());
dump($fooOtherDB->first());
```

Which gives you:

```
^ LaracraftTech\LaravelDynamicModel\DynamicModel_mysql_foo {#328 ▼
  #connection: "mysql"
  #table: "foo"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #attributes: array:5 [▼
    "id" => 1
    "col1" => "asdf"
    "col2" => 123
    "created_at" => "2023-03-22 15:34:22"
    "updated_at" => "2023-03-22 15:34:22"
  ]
}

^ LaracraftTech\LaravelDynamicModel\DynamicModel_mysql_faz {#328 ▼
  #connection: "mysql"
  #table: "faz"
  #primaryKey: "period"
  #keyType: "string"
  +incrementing: false
  #attributes: array:5 [▼
    "period" => "2023-01-01"
    "col1" => "asdf"
    "col2" => 123
    "created_at" => "2023-03-22 15:34:22"
    "updated_at" => "2023-03-22 15:34:22"
  ]
}

^ LaracraftTech\LaravelDynamicModel\DynamicModel_mysql2_foo {#328 ▼
  #connection: "mysql2"
  #table: "foo"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #attributes: array:5 [▼
    "id" => 1
    "col1" => "asdf"
    "col2" => 123
    "created_at" => "2023-03-22 15:34:22"
    "updated_at" => "2023-03-22 15:34:22"
  ]
}

```

### Use your own Dynamic Model

[](#use-your-own-dynamic-model)

---

If you need to add **custom** methods to your Dynamic Model or maybe **guard** it, you can just create your own Eloquent model and then call it through the **Factory**. This will create a new Model instance, which is **extended** by your original model. Make sure your model is implementing the **DynamicModelInterface** or is extended by the **DynamicModel** class.

```
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use LaracraftTech\LaravelDynamicModel\DynamicModel;
use LaracraftTech\LaravelDynamicModel\DynamicModelInterface;

// class MyDynamicModel extends Model implements DynamicModelInterface (this would also work)
class MyDynamicModel extends DynamicModel
{
    proteced $guarded = ['id'];

    public function doSomething()
    {
        // do something
    }
}

$foo = app(DynamicModelFactory::class)->create(MyDynamicModel::class, 'foo');

$foo->create([
    'col1' => 'asdf',
    'col2' => 123
]);

$foo->doSomething();

dd($foo->first());
```

Which gives you:

```
^ App\Model\MyDynamicModel_mysql_foo {#328 ▼
  #connection: "mysql"
  #table: "foo"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #attributes: array:5 [▼
    "id" => 1
    "col1" => "asdf"
    "col2" => 123
    "created_at" => "2023-03-22 15:34:22"
    "updated_at" => "2023-03-22 15:34:22"
  ]
  ...
}

```

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Zacharias Creutznacher](https://github.com/laracraft-tech)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

55

—

FairBetter than 97% of packages

Maintenance86

Actively maintained with recent releases

Popularity36

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 63% 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

Every ~117 days

Recently: every ~271 days

Total

12

Last Release

95d ago

Major Versions

v1.0.2 → v2.0.02023-04-02

v2.1.0 → v3.0.02023-04-13

v3.0.4 → v4.0.02026-04-01

PHP version history (3 changes)v1.0.0PHP &gt;=7.4

v2.0.0PHP ^8.1

v4.0.0PHP ^8.2

### Community

Maintainers

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

![](https://www.gravatar.com/avatar/65c03df85c364040f9a6586a938c162ec0880a5f1f1ae9a119241abbbc189d26?d=identicon)[laracraft-tech](/maintainers/laracraft-tech)

---

Top Contributors

[![Sairahcaz](https://avatars.githubusercontent.com/u/7384870?v=4)](https://github.com/Sairahcaz "Sairahcaz (75 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (27 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (16 commits)")[![Skop-22](https://avatars.githubusercontent.com/u/100052537?v=4)](https://github.com/Skop-22 "Skop-22 (1 commits)")

---

Tags

dynamic-modeldynamic-schemadynamic-tableeloquentlaravelmodelphplaravellaracraft-techlaravel-dynamic-model

###  Code Quality

TestsPest

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/laracraft-tech-laravel-dynamic-model/health.svg)

```
[![Health](https://phpackages.com/badges/laracraft-tech-laravel-dynamic-model/health.svg)](https://phpackages.com/packages/laracraft-tech-laravel-dynamic-model)
```

###  Alternatives

[spatie/laravel-medialibrary

Associate files with Eloquent models

6.1k43.2M633](/packages/spatie-laravel-medialibrary)[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[yajra/laravel-oci8

Oracle DB driver for Laravel via OCI8

8793.2M25](/packages/yajra-laravel-oci8)[spatie/laravel-health

Monitor the health of a Laravel application

87512.0M167](/packages/spatie-laravel-health)[glushkovds/phpclickhouse-laravel

Adapter of the most popular library https://github.com/smi2/phpClickHouse to Laravel

2051.5M2](/packages/glushkovds-phpclickhouse-laravel)[clickbar/laravel-magellan

This package provides functionality for working with the postgis extension in Laravel.

440902.9k1](/packages/clickbar-laravel-magellan)

PHPackages © 2026

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