Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MattB's avatar
Level 2

SQL/Eloquent how to add a day to a date column

I have the following command which searches the 'from' column and returns rows which the 'from' column is before today and 'to' is after today:

$on_hire = StockBooking::where('from','<=',Carbon::now())->where('to','>=',Carbon::now()->format('Y-m-d'))->get();

This is all working ok, but it's changed so that I need to use the 'from' column plus one day instead of just 'from'. I have the below raw sql statement but this doesn't seem to return any rows:

 $on_hire = DB::table('stock_booking')
            ->where(DB::raw('DATE_ADD(from, INTERVAL 1 DAY)'), '<=', Carbon::now())
            ->where('to','>=',Carbon::now()
            ->format('Y-m-d'))
            ->get();

Could you possibly tell me where I've gone wrong please?

0 likes
6 replies
MichalOravec's avatar

Try it with whereRaw

 $on_hire = DB::table('stock_booking')
            ->whereRaw('DATE_ADD(from, INTERVAL 1 DAY) <= NOW()')
            ->where('to', '>=', now()->toDateString())
            ->get();
MattB's avatar
Level 2

Yeah I don't think it likes 'from'. Is that a reserved word? If so how would I get around it?

MattB's avatar
Level 2

I'm getting this error:

[ERROR] SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from, INTERVAL 1 DAY) <= NOW() and `to` >= ?' at line 1 (SQL: select * from `stock_booking` where DATE_ADD(from, INTERVAL 1 DAY) <= NOW() and `to` >= 2021-02-02)
MattB's avatar
Level 2

Is that really the only way, is there no way to use an alias for it? Or escape the word somehow?

MichalOravec's avatar
Level 75

Or maybe it will work with backsticks

 $on_hire = DB::table('stock_booking')
            ->whereRaw('DATE_ADD(`from`, INTERVAL 1 DAY) <= NOW()')
            ->where('to', '>=', now()->toDateString())
            ->get();

Please or to participate in this conversation.