Level 75
An example of querying a query https://laracasts.com/discuss/channels/eloquent/calculate-average-of-multiple-fields-eloquent?page=1&replyId=514663
Note max can be tricky and give unexpected results.
Hi, I use this eloquent code to get data:
$PointByStudentHabit = HabitRecord::with('user')->join('habits', 'habit_id', '=', 'habits.id')->where('course_id', $course->id)->where('state', 1)->whereIn('user_id', $enrolledUserId)->select('user_id', 'habit_id', 'max', 'habits.points', DB::raw("count(*) as count"))->groupBy('user_id', 'habit_id')->get();
and I want to make a custom calculation based on a condition which is:
$PointByStudentHabit->count > $PointByStudentHabit->max ? $PointByStudentHabit->total = ($PointByStudentHabit->points * $PointByStudentHabit->max) : $PointByStudentHabit->total = ($PointByStudentHabit->points * $PointByStudentHabit->count);
As you note I have set the collection in new attribute, and that going good, the problem is that I want to group the eloquent collection by habit_id but I can not do, I have some code with no effort, is there a way to do it ?
Please or to participate in this conversation.