mardi 21 juin 2016

Grouping on a related table Eloquent/Laravel

I have two tables:

checks
id time (DateTime)

views
id check_id amount

At the moment I have this query to retrieve all the data for past 24 hours:

$dot_checks = View::whereHas('check', function($q) {
                                $q->where('time', '>', DB::raw('NOW() - INTERVAL 24 HOUR'));
                                $q->orderBy('time');
                            })->get();

I have checks every minute, but for display purpouses I need to group them by 10 minutes, taking average of the amount.

I'm struggling to do this grouping, where the key ('time') is on the related table. Could someone show me the right way to do it.

Thank you fro your time.

Aucun commentaire:

Enregistrer un commentaire