laravel/framework — pull request #5350 by crynobone (2014-08-06 04:15:57+00:00)
Title: Allow Query Builder to explicitly force use write pdo during SELECT query

| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | N/A
| License | MIT
| Doc PR | N/A

## Current Behaviour

Right now there is no option to enforce use of "write" pdo connection when using SELECT. The only way to use it is to either have a [separate connection](http://laravel.io/forum/05-25-2014-read-write-mysql-connection-forcing-a-select-to-use-write-connection), use `DB::statement()` or wrap the code inside `DB::transaction()`.

## Proposed Behaviour

```php
// Get recently register user and activate him/her
$query = DB::table('users')->where('id', '=', $id)->where('activation_token', '=', $token)->first();
```

Depending on the update latency between "master" and "slave", you might not be able to instantly activate a new user if the data is not yet available on the "slave" database.

```php
// Get recently register user and activate him/her
$query = DB::table('users')->where('id', '=', $id)->where('activation_token', '=', $token)->onWritePdo()->first();
```

Add `->onWritePdo()` chain method when building SELECT statement to force use of "write" pdo connection. This helper should only valid for the current SELECT statement and will be reset when `Illuminate\Database\Query\Builder::get()` is called. Other than that, it should also consider when building a paginated query where the COUNT aggregator should respect the same connection as the SELECT, hence `continueOnWritePdo()`.

## Notes:

* Not sure whether the method naming or the document really fit, so open for suggestion.

Signed-off-by: crynobone