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.
统计信息
- 总下载量: 33
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-02-16