Download the PHP package jackbayliss/laravel-model-connection without Composer

On this page you can find all versions of the php package jackbayliss/laravel-model-connection. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-model-connection

Laravel Model Connections

Latest Version on Packagist GitHub Tests Action Status

Define separate read and write database connections per Eloquent model.

Why?

Laravel has built-in support for read/write connection splitting, but it's configured at the connection level in config/database.php which means, every model using that connection shares the same read/write behaviour:

There's no first-party way to set specific models to only read. The alternative is manually calling ::on() or ->setConnection() everywhere you query, which is easy to forget and scatters connection logic across your codebase:

This package gives you control over read and write connections per model without touching your connection config, and without sprinkling ::on() calls throughout your code. This is extremely useful if you know certain models can be deferred to a replica, and certain models are required instantly.

Supports

Installation

Usage

Add the HasReadWriteConnection trait to your model and define $readConnection and $writeConnection:

All reads (first(), get(), relationships, eager loads) will go to mysql_replica, and all writes (create(), update(), delete()) will go to mysql_primary.

If $readConnection or $writeConnection are not set, the trait falls back to the model's default $connection.

Enum Support

You can use a backed enum instead of a plain string:

Runtime Override

You can override the connections at runtime using the fluent setters:

Available Methods

Method Description
getReadConnection() Returns the Connection instance for reads
getWriteConnection() Returns the Connection instance for writes
getReadConnectionName() Returns the read connection name as a string
getWriteConnectionName() Returns the write connection name as a string
setReadConnection($connection) Override the read connection at runtime
setWriteConnection($connection) Override the write connection at runtime

Using with Factories

By default, factories will write to your model's $writeConnection (default if empty). In a real application this is correct as replication will sync the data to your read replica.

However, in tests you typically want factory-created records to be immediately readable via the model. Since reads go to $readConnection, you need factory writes to land there too.

Add the HasReadWriteConnectionFactory trait to your factory:

This redirects the factory's save to the read connection, so User::factory()->create() produces records that User::find(), User::get(), etc. can retrieve.

If you already have a configure() method, call withReadWriteConnection() inside it instead:

Testing

The package includes a full test suite covering read/write routing, enum support, runtime overrides, and fallback behaviour. To run the tests:

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.


All versions of laravel-model-connection with dependencies

PHP Build Version
Package Version
Requires php Version ^8.3
illuminate/database Version ^12.0|^13.0
illuminate/support Version ^12.0|^13.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package jackbayliss/laravel-model-connection contains the following files

Loading the files please wait ...