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(10mo ago)0106MITPHP

Since Feb 16Pushed 10mo 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 today

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

29

—

LowBetter than 57% of packages

Maintenance53

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

327d 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

[jdorn/sql-formatter

a PHP SQL highlighting library

3.9k117.2M118](/packages/jdorn-sql-formatter)[propel/propel1

Propel is an open-source Object-Relational Mapping (ORM) for PHP5.

8351.6M87](/packages/propel-propel1)[jfelder/oracledb

Oracle DB driver for Laravel

11518.4k](/packages/jfelder-oracledb)

PHPackages © 2026

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