Last active
August 2, 2020 19:23
-
-
Save mohammadYousefiDev/c394388da602ccf11a9fbb7041488b55 to your computer and use it in GitHub Desktop.
Group by in laravel withcount orm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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