PHPackages                             megaezz/laravel-database-prefix - 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. megaezz/laravel-database-prefix

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

megaezz/laravel-database-prefix
===============================

Trait to add database prefix to eloquent models.

1.0.2(9mo ago)074MITPHP

Since Feb 16Pushed 9mo ago1 watchersCompare

[ Source](https://github.com/megaezz/laravel-database-prefix)[ Packagist](https://packagist.org/packages/megaezz/laravel-database-prefix)[ RSS](/packages/megaezz-laravel-database-prefix/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)DependenciesVersions (4)Used By (0)

Laravel Database Prefix
=======================

[](#laravel-database-prefix)

**Laravel Database Prefix** is a package that allows you to dynamically add a database prefix to Eloquent models, including specifying the database name. This is particularly useful when multiple databases are used within the same database connection in Laravel.

By leveraging this package, you can properly define relationships between models that belong to different databases, making **cross-database relations** possible—something that Laravel does not support out of the box.

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

[](#installation)

Install the package via Composer:

```
composer require megaezz/laravel-database-prefix
```

Usage
-----

[](#usage)

Assume you have a database connection defined in `config/database.php`:

```
'mysql' => [
    ...
],
```

Within this connection, you have multiple databases: `default`, `database1`, and `database2`. You want to use Eloquent models where the corresponding tables are located in different databases within this same connection.

### Example

[](#example)

Define a `User` model that belongs to `database1`:

```
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Megaezz\LaravelDatabasePrefix\HasDatabasePrefix;

class User extends Model
{
    use HasDatabasePrefix;

    protected $database = 'database1';

    public function articles()
    {
        return $this->hasMany(Article::class);
    }
}
```

Define an `Article` model that belongs to `database2`:

```
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Megaezz\LaravelDatabasePrefix\HasDatabasePrefix;

class Article extends Model
{
    use HasDatabasePrefix;

    protected $database = 'database2';
}
```

### Querying Across Databases

[](#querying-across-databases)

Now, the following query will work correctly, even though `users` and `articles` are in different databases:

```
User::has('articles')->get();
```

License
-------

[](#license)

This package is open-source and available under the MIT license.

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance58

Moderate activity, may be stable

Popularity12

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity40

Maturing project, gaining track record

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 ~88 days

Total

3

Last Release

272d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/63825aaf3a826f829de7c1be507bc540ab155bd7f52379416348a2b828626b54?d=identicon)[megaezz](/maintainers/megaezz)

### Embed Badge

![Health badge](/badges/megaezz-laravel-database-prefix/health.svg)

```
[![Health](https://phpackages.com/badges/megaezz-laravel-database-prefix/health.svg)](https://phpackages.com/packages/megaezz-laravel-database-prefix)
```

###  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.3k](/packages/illuminate-database)[mongodb/mongodb

MongoDB driver library

1.6k64.0M545](/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)
