Skip to content

Instantly share code, notes, and snippets.

@mohammadYousefiDev
Last active August 2, 2020 19:23
Show Gist options
  • Save mohammadYousefiDev/c394388da602ccf11a9fbb7041488b55 to your computer and use it in GitHub Desktop.
Save mohammadYousefiDev/c394388da602ccf11a9fbb7041488b55 to your computer and use it in GitHub Desktop.
Group by in laravel withcount orm
<?php
//in Model.php
public function contributes()
{
return $this->hasmany('\App\ExamUserSelect', 'exam_id');
}
/* in controller
* grouped by group_id
*
*/
return Model::withcount(['contributes' => function($q){
$q->select( \DB::raw('count(distinct(group_id))') );
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment