2

I am using Laravel

Let's say, I have two date fields in my table. If I want to compare them, i can do whereRaw clause.

$query->whereRaw('date1 > date2')->get()

Is there a way to make a modification to date2 inside this query, so it is actually date2-1day?

Something like:

$query->whereRaw('date1 > (date2-1day)')->get()

2 Answers 2

3

You are free to call any SQL code in the "raw" part of your query, so you could do sth like below:

$query->whereRaw('date1 > DATE_SUB(date2, INTERVAL 1 DAY)')->get();

Keep in mind that executing SQL code this way will make your queries work only in databases that support such functions.

Sign up to request clarification or add additional context in comments.

Comments

0

Another way would be using whereColumn like

$users = DB::table('users')
             ->whereColumn('updated_at', '>', 'created_at')
             ->get();

OR

UserTable::whereRaw('column1 != column2')->get();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.