Huzoor Bux
Huzoor Bux

Reputation: 1038

Laravel considering variable as column name in query builder

I am using laravel 5.5 and checking event checking if slot available between 2 times using this query:

$schedules->where('id', $id)
                ->wherebetween($date, ['start','end'])
                ->orwherebetween($endTime, ['start','end'])
                ->orwherebetween('start', [$date,$endTime])
                ->orderBy('start')->get(); 

getting this error

"message": "SQLSTATE[42S22]: Column not found: 1054 Unknown column '2019-03-06 13:00:00' in 'where clause'

Upvotes: 0

Views: 741

Answers (2)

Huzoor Bux
Huzoor Bux

Reputation: 1038

I have fixed that by adding DB::raw() read more

$schedules->where('id', $id)
                ->wherebetween(DB::raw($date), ['start','end'])
                ->orwherebetween(DB::raw($endTime), ['start','end'])
                ->orwherebetween('start', [$date,$endTime])
                ->orderBy('start')->get(); 

Upvotes: 0

Kapitan Teemo
Kapitan Teemo

Reputation: 2164

its because the 1st parameter should be the name of the column:

you should change your code to:

->wherebetween('column name', ['1st date','2nd date'])
->orwherebetween('column name', ['1st date','2nd date'])

Upvotes: 1

Related Questions