megaezz/laravel-database-prefix 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

megaezz/laravel-database-prefix

最新稳定版本:1.0.2

Composer 安装命令:

composer require megaezz/laravel-database-prefix

包简介

Trait to add database prefix to eloquent models.

README 文档

README

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

Install the package via Composer:

composer require megaezz/laravel-database-prefix

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

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

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

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

License

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

统计信息

  • 总下载量: 90
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固