abdullah2almuqbil's avatar

add new attribute to a collection and group it ?

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 ?

0 likes
1 reply

Please or to participate in this conversation.