Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you wrap Laravel Eloquent ORM query scopes in parentheses when chaining?

Tags:

In Eloquent, I'd like to generate this query:

SELECT * FROM table WHERE a=1 AND ( b=2 OR c=3 ); 

But I seem to be generating this query instead:

SELECT * FROM table WHERE a=1 AND b=2 OR c=3; 

Here is my implementation and code:

$result = Model::aIsOne()->bIsTwoOrCIsThree()->get(); 

Model contains:

function scopeAIsOne($query) {     return $query->where('a', 1); }  function scopeBIsTwoOrCIsThree($query) {     return $query->where('b', 2)->orWhere('c', 3); } 

Thanks for any help. I've searched through the docs including Advanced Wheres to no avail.

like image 355
crowsfan85 Avatar asked Jan 03 '14 23:01

crowsfan85


1 Answers

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(); 
like image 131
Martins Balodis Avatar answered Oct 10 '22 22:10

Martins Balodis