Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zhanang19/83378a988ca5d8fe9017b6a12342c3d7 to your computer and use it in GitHub Desktop.
Save zhanang19/83378a988ca5d8fe9017b6a12342c3d7 to your computer and use it in GitHub Desktop.
Laravel order by relationship aggregates example
<?php
$popularSerials = Serial::addSelect([
'total_views' => Video::selectRaw('sum(views) as total_views')
->whereColumn('serial_id', 'serials.id')
->groupBy('serial_id')
])
->orderBy('total_views')
->get();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment