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

triadi's avatar
Level 1

Querying PostgreSQL Range Date Column Type with LOWER function Always Return Empty Data, but Query Works in PostgreSQL

Hi everyone,

I'm facing an issue where a query in Laravel, using both Eloquent and Query Builder, always returns empty results, even though the same query works perfectly fine when run directly in PostgreSQL (using DataGrip).

Here’s the code I’m using in Laravel: Using Eloquent:

$pertemuans = Pertemuan::query()
    ->whereRaw('LOWER(waktu) BETWEEN ? AND ?', [
        '2024-09-23 02:41:18',
        '2024-09-23 02:43:18'
    ])
    ->get();

Using Query Builder:

$pertemuans = DB::table('kelas.pertemuan')
    ->whereRaw('LOWER(waktu) BETWEEN ? AND ?', [
        '2024-09-23 02:41:18',
        '2024-09-23 02:43:18'
    ])
    ->get();

However, both queries return empty results, but when I try the same query directly in PostgreSQL, it works fine and returns the expected data:

SELECT * FROM "kelas"."pertemuan"
WHERE LOWER(waktu) BETWEEN '2024-09-23 02:41:18' AND '2024-09-23 02:43:18';

Additional Information:

  • Database: PostgreSQL
  • Laravel version: 9.52.4
  • PHP version: 8.3.3
  • Data type of the waktu column: tstzrange

Question: Why does this query work in PostgreSQL but always return empty data in Laravel? Has anyone encountered this issue before or have any suggestions for a fix?

Thank you so much for your help!

0 likes
2 replies
Snapey's avatar

Why do you need to use Lower?

use Carbon for best portability

$pertemuans = Pertemuan::whereBetween('waktu',[$start, $end])->get();
triadi's avatar
Level 1

@Snapey My purpose in using LOWER is that I want to retrieve the lower bound of the waktu (time) column. Then, I want to get the data where the lower bound of the time falls within the specified range.

For example, if the value of the waktu column is:

["2024-09-23 02:42:00+00", "2024-09-23 11:40:00+00"]

I want to check if 2024-09-23 02:42:00+00 is within the range between '2024-09-23 02:41:18' and '2024-09-23 02:43:18'.

Please or to participate in this conversation.