Curious why you cannot change it? Maybe replace it then? Write a completely new query that replaces the old variable?
Jan 4, 2023
5
Level 8
Is it possible to add a global `AND` condition to an Eloquent query?
Hello
I have this simple query that I cant change it unfortunately but I can add on it :
echo User::orWhereIn('id',[1,2,5])
->orWhereIn('id',[1,92,5,9,6,96,9])
->toSql();
SQL result is :
select * from "users" where ("id" in (?, ?, ?) or "id" in (?, ?, ?, ?, ?, ?, ?)) and "users"."deleted_at" is null
In this case the where parentheses has "id" in (?, ?, ?) or "id" in (?, ?, ?, ?, ?, ?, ?) which is okay
The problem is when I add ->where('name','ilike','%mod%')
the result is:
echo User::orWhereIn('id',[1,2,5])
->orWhereIn('id',[1,92,5,9,6,96,9])
->where('name','ilike','%mod%')
->toSql();
SQL result is :
select * from "users" where ("id" in (?, ?, ?) or "id" in (?, ?, ?, ?, ?, ?, ?) and "name"::text ilike ?) and "users"."deleted_at" is null
The sql result I need is : ...where( ... OR ...) AND "name"::text ilike ?
select * from "users" where ("id" in (?, ?, ?) or "id" in (?, ?, ?, ?, ?, ?, ?)) and "name"::text ilike ? and "users"."deleted_at" is null
Hope it's clear!
Any idea ???
Please or to participate in this conversation.