Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Eloïse's avatar

group sub collection and get count of similar items

Hello, I'm not able to find a solution to this , after an eloquent query I get this collection :

/// query
$openQuestions = Question::withCount(['answers'])
                ->with(['answers','purposesThroughAnswers'])

                ->wherehas('answers',function($q)use ($session){
                    $q->where('session_id',$session->id);
                })
                ->where('satisfiable',false)
                ->get();

/// result
array:1 [▼ // app/Http/Controllers/SessionController.php:127
  0 => array:10 [▶
    "id" => 6
    "question" => "À l'issue de cette intervention, quel est votre ressenti ? "
    "image" => "buddies"
    "satisfiable" => 0
    "created_at" => "2022-12-06T18:12:01.000000Z"
    "updated_at" => "2022-12-06T18:12:01.000000Z"
    "deleted_at" => null
    "answers_count" => 3
    "answers" => array:3 [▶
      0 => array:9 [▶
        "id" => 6
        "participant_id" => 1
        "question_id" => 6
        "purpose_id" => 22
        "session_id" => 1
        "comment" => "Ujdjjsjs"
        "created_at" => "2022-12-06T18:14:16.000000Z"
        "updated_at" => "2022-12-06T18:14:16.000000Z"
        "deleted_at" => null
      ]
      1 => array:9 [▶
        "id" => 12
        "participant_id" => 2
        "question_id" => 6
        "purpose_id" => 21
        "session_id" => 1
        "comment" => null
        "created_at" => "2022-12-07T12:27:51.000000Z"
        "updated_at" => "2022-12-07T12:27:51.000000Z"
        "deleted_at" => null
      ]
      2 => array:9 [▶
        "id" => 18
        "participant_id" => 3
        "question_id" => 6
        "purpose_id" => 21
        "session_id" => 1
        "comment" => null
        "created_at" => "2022-12-07T13:11:24.000000Z"
        "updated_at" => "2022-12-07T13:11:24.000000Z"
        "deleted_at" => null
      ]
    ]
    "purposes_through_answers" => array:3 [▶
      0 => array:9 [▶
        "id" => 22
        "question_id" => 6
        "label" => "Rassuré(e)"
        "icon" => "icon-fontawesome.svgs.solid.face-smile-beam"
        "satisfied" => 0
        "created_at" => "2022-12-06T18:12:01.000000Z"
        "updated_at" => "2022-12-06T18:12:01.000000Z"
        "deleted_at" => null
        "pivot" => array:2 [▶
          "question_id" => 6
          "purpose_id" => 22
        ]
      ]
      1 => array:9 [▶
        "id" => 21
        "question_id" => 6
        "label" => "Reconnaissant(e)"
        "icon" => "icon-fontawesome.svgs.regular.face-grin-stars"
        "satisfied" => 0
        "created_at" => "2022-12-06T18:12:01.000000Z"
        "updated_at" => "2022-12-06T18:12:01.000000Z"
        "deleted_at" => null
        "pivot" => array:2 [▶
          "question_id" => 6
          "purpose_id" => 21
        ]
      ]
      2 => array:9 [▶
        "id" => 21
        "question_id" => 6
        "label" => "Reconnaissant(e)"
        "icon" => "icon-fontawesome.svgs.regular.face-grin-stars"
        "satisfied" => 0
        "created_at" => "2022-12-06T18:12:01.000000Z"
        "updated_at" => "2022-12-06T18:12:01.000000Z"
        "deleted_at" => null
        "pivot" => array:2 [▶
          "question_id" => 6
          "purpose_id" => 21
        ]
      ]
    ]
  ]
]

My wish is to group the sub collection 'purposes_through_answers' by id and get the count of items in groups which should be 2 for id = 21. How can I achieve this ?

thanks for your help

0 likes
1 reply
Eloïse's avatar
Eloïse
OP
Best Answer
Level 2

I solved it this way in the view :

@foreach($openQuestion->purposesThroughAnswers->countBy('label') as $label => $countInGroup)

Please or to participate in this conversation.