Question

My Query is like

SELECT * FROM activities WHERE (active=1 AND linked_project_id IN (2,3) AND GLOBAL=1 OR (linked_project_id=2  AND active=1))

But I dont Know how to put 'linked_project_id=2 AND active=1' this inside AND with Laravel. Here is my code for doing-

$activities=  Activitie::where('active','=',1)->whereIn('linked_project_id',array(2,3))->where('global','=','1')->orWhere('linked_project_id','='2)->where('active','=',1)->get();

but this is producing result like

SELECT * FROM activities WHERE (active=1 AND linked_project_id IN (2,3) AND GLOBAL=1 OR (linked_project_id=2 ) AND active=1)
Was it helpful?

Solution

Make use of Advanced Wheres.

Model::where(function ($query) {
    $query->where('a', '=', 1)
          ->orWhere('b', '=', 1);
})->where(function ($query) {
    $query->where('c', '=', 1)
          ->orWhere('d', '=', 1);
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top