Intend base query #58449
-
|
I'm looking for intent-based Eloquent queries that route to read/write connections User::read()->where('active', 1)->get(); // uses read connection
User::write()->whereKey($id)->update([...]); // uses write connection |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
|
But isn't that redundant? I mean, User::withSeparateReadWriteConnection()->where('active', 1)->get(); // uses read connection
User::withSeparateReadWriteConnection()->whereKey($id)->update([...]); // uses write connectionor User::withSeparateReadWriteConnection(fn (Builder $query) => $query->where('active', 1)->get()); // uses read connection
User::withSeparateReadWriteConnection(fn (Builder $query) => $query->whereKey($id)->update([...])); // uses write connectionor class User extends Model
{
protected bool $withSeparateReadWriteConnection = true;
}
User::query()->where('active', 1)->get(); // uses read connection
User::query()->whereKey($id)->update([...]); // uses write connectionI know, the method/property name is a mouthful, but it's just for demonstration purposes 😉 |
Beta Was this translation helpful? Give feedback.
-
|
User::setConnection('mysql.read')->where('active', 1)->get(); // uses read connection
User::setConnection('postgresql.write')->whereKey($id)->update([...]); // uses write connectionEdit: Use #58449 (comment) |
Beta Was this translation helpful? Give feedback.
-
Model::query()->useWritePdo()->... |
Beta Was this translation helpful? Give feedback.
But isn't that redundant? I mean,
get()could automatically choosereadandupdate()could automatically choosewrite:or
or