How do you wrap Laravel Eloquent ORM query scopes in parentheses when chaining?
You can generate parentheses by passing a callback function to where()
.
Model::where('a',1)->where(function($query) {
$query->where('b', 2)->orWhere('c',3);
})->get();
SEE EDIT FOR FINAL ANSWER
Better than raw, use orWhere with a closure:
$model = Model::whereAIsOne()->orWhere(function($query) {
$query->where('b', 1)->where('c', 1);
})->get();
The really unfortunate thing (and the way I ended up at this post) is that the second $query used in the closure is an instance of \Illuminate\Database\Query\Builder rather than \Illuminate\Database\Eloquent\Builder - this means that you can't use the model scopes within that closure, because Query\Builder instances don't have a reference to a model.
That said, I still think this is much more ORM-like and useful as a developer.
EDIT
Took a while to figure out, but this will work if you want to use your scopes as well:
$model = Model::whereAIsOne()->orWhere(function($query) {
$this->newEloquentBuilder($query)->setModel($this)->whereBIsOneAndCIsOne();
})->get();
I'm actually creating a fourth scope in my code that encompasses all of this, so the $this
might not work in this context - however, I find that scoping for my models is one of the most important parts of building a good system.
This definitely should be built into Eloquent - the engine has a good start, but so many features are missing.