laravel/framework — pull request #5974 by bostanio (2014-10-04 18:17:13+00:00)
Title: Defer query builder bindings until the query is being compiled

The old design of adding bindings as clauses are added to the query worked most of the time had two major issues:
1. For some complex queries, like unions or sub queries involving joins, the bindings fail to apply or are applied in the wrong order. See #5833 for examples.
1. Cleanly removing global scopes that introduced bindings is impossible to guarantee, especially if other global scopes are in use.

To fix this we take a very simple approach to defer assigning the query bindings until the query is being compiled. This has two effects:
1. Query bindings are guaranteed to be added in the same order that the sql string is concatenated.
1. When removing global scopes you don't need to worry about removing bindings. Just remove the appropriate query clauses and the bindings will be taken care of at compile time.