How to fetch data one line per each 15 minutes from the last 24 hour in Laravel 5.8?

Viewed 257

I'm trying to get logs from the last 24 hours for a chart I'm building. I'm using Laravel 5.8.

I need 1 line per every 15 minutes for the chart to be accurate.

The current state is one line per hour, I can't think of out to turn it into 15 minutes.

Please help me to make it work and make the code look as pretty as possible!

public function getDataDay(&$data) {
    $data = $data->where('created_at', '>', Carbon::now()->subHours(23)->format('Y-m-d H:i:s'))
                  ->select('id', 'part_id', 'data', 'created_at')
                  ->selectRaw('HOUR(created_at) as hour')
                  ->groupBy('hour')
                  ->get();
}
1 Answers

use this, it is what i use in mysql

->selectRaw('FROM_UNIXTIME(FLOOR( UNIX_TIMESTAMP(created_at)/900 ) * 900) AS quaterhour')
->groupBy('quaterhour')

Of Course if you have mire than one day, you have to group also by years , month and days

Related