elysiumrealms/database-router

A Database Router isolate Read Write Connections

1.0.1 2024-06-09 13:20 UTC

This package is auto-updated.

Last update: 2025-03-09 14:51:26 UTC


README

Description

Database Router is an extension for databases using master-slave architecture. This extension ensures that the default connection uses a read-write capable database host and extends an optional router connection to partially apply read-write isolation strategies based on business logic.

Installation

  1. Install elysiumrealms/database-router.

    composer require elysiumrealms/database-router
  2. Configure config/database.php and config/app.php.

    php artisan db:router:install

Read from DB_READ_HOST

To perform read operations using the read host, you can use the following methods:

$model = Model::router()->first();
$model = Admin::where('id', 1)->router()->first();
$model = Admin::router()->where('id', 1)->first();
DB::table('users')->router()->find(1);

Update with DB_WRITE_HOST

To perform update operations using the write host, you can use the following methods:

$model = Model::router()
    ->update(['name' => 'new-name']);
$model = Admin::where('id', 1)->router()
    ->update(['name' => 'new-name']);
$model = Admin::router()->where('id', 1)
    ->update(['name' => 'new-name']);
DB::table('users')->router()
    ->update(['name' => 'new-name']);