'Laravel SQL query midnight time not showing

I am making a schedule display system and it shows the schedules of ferries. my problem is if I dont put midnight time in order then query skips midnight time and displays morning instead

EXAMPLE:-

if i put data in order like this

  1. 1:30 am
  2. 6:30 am
  3. 7:30 am

then the times show correctly, but if its not in order

  1. 6:30 am
  2. 7:30 am
  3. 1:30 am

then the query skips the 1:30 am and goes straight to 6:30 to display.

Here is my laravel SQL Query:- if departure time is greater than current time then show the next available one

         DB::table('ferries')->groupBy('ferry_name')
        ->whereTime('departure_time', '>', Carbon::now()->toTimeString())
        ->whereIn('schedule_type', ['Weekday'])
        ->where('terminal_name', $terminal_name)->get()->all();


Solution 1:[1]

Resukltsets are by nature unsorted yo you need to add a sorting order

This would sort ascending see manual

DB::table('ferries')->groupBy('ferry_name')
->whereTime('departure_time', '>', Carbon::now()->toTimeString())
->whereIn('schedule_type', ['Weekday'])
->where('terminal_name',$terminal_name)->orderBy('departure_time')->get()->all()

Solution 2:[2]

Used unique() instead of groupBy() then the orderBy worked not sure why.

$posts =  DB::table('ferries')->orderBy('departure_time', 'asc')
            ->where('departure_time', '>', Carbon::now()->toTimeString())
            ->whereIn('schedule_type', ['Weekday'])
            ->where('terminal_name', $terminal_name)->get()->unique('ferry_name')->all();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Navy