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(1y ago)0111MITPHP

Since Feb 16Pushed 1y 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 1w 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

28

—

LowBetter than 51% of packages

Maintenance49

Moderate activity, may be stable

Popularity11

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity41

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

373d 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.8k117.8M121](/packages/jdorn-sql-formatter)[backup-manager/backup-manager

A framework agnostic database backup manager with user-definable procedures and support for S3, Dropbox, FTP, SFTP, and more with drivers for popular frameworks.

1.7k1.6M11](/packages/backup-manager-backup-manager)[propel/propel1

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

8351.6M88](/packages/propel-propel1)[insolita/yii2-migration-generator

Set of gii tools for generating files for migration by schema of table , phpdoc or table data

108508.0k5](/packages/insolita-yii2-migration-generator)[xpdo/xpdo

A PDO-based Object/Relational Bridge Library

7088.4k4](/packages/xpdo-xpdo)[voku/session2db

A PHP library acting as a wrapper for PHP's default session handling functions which stores data in a MySQL database, providing both better performance and better security and protection against session fixation and session hijacking.

2920.0k](/packages/voku-session2db)

PHPackages © 2026

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