Home > Mobile >  Laravel SQL query midnight time not showing
Laravel SQL query midnight time not showing

Time:04-11

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();

CodePudding user response:

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()

CodePudding user response:

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();
  • Related