John Davidson

php - Laravel controller getting ids from array at the returned collection

0 comments
Message:


I have php laravel framework project (laravel-8) and at DB , I have column which has array of objects like this:


[
{
"id": "G0",
"name": "Social-Emotional Understanding",
"questions": [
{
"gId": "G0",
"id": 1,
"question_id": 34,
"question_title": "Age - appropriate social- emotional understanding. Clearly understands facial expressions, gestures, tone of voice, and body language of\r\nothers. Able to under - stand that others may have a different perspective and what that perspective may be.",
"question_note": null,
"answers": [
{
"answer_index": "1",
"answer_text": "yes",
"answer_image": null,
"answers_redirection_group_index": "G1",
"answers_redirection_group_question": "noQuestion",
"degree": "1"
},
{
"answer_index": "2",
"answer_text": "No",
"answer_image": null,
"answers_redirection_group_index": "noGroup",
"answers_redirection_group_question": "noQuestion",
"degree": 0
},
{
"answer_index": "3",
"answer_text": "N/A",
"answer_image": null,
"answers_redirection_group_index": "noGroup",
"answers_redirection_group_question": "noQuestion",
"degree": 0
}
]
},
{
"gId": "G0",
"id": 2,
"question_id": 35,
"question_title": "Mildly impaired social-emotional understanding. Responsive to most facial expressions and expressions of emotion in others’ gestures and\r\nbody language, but these cues may need to be slightly exaggerated. Subtler expressions such as mild sarcasm, doubt, or ambiguity are\r\nsometimes not understood. The ability to take another’s perspective is inconsistent.",
"question_note": null,
"answers": [
{
"answer_index": "1",
"answer_text": "yes",
"answer_image": null,
"answers_redirection_group_index": "G1",
"answers_redirection_group_question": "noQuestion",
"degree": "2"
},
{
"answer_index": "2",
"answer_text": "No",
"answer_image": null,
"answers_redirection_group_index": "noGroup",
"answers_redirection_group_question": "noQuestion",
"degree": 0
},
{
"answer_index": "3",
"answer_text": "N/A",
"answer_image": null,
"answers_redirection_group_index": "G1",
"answers_redirection_group_question": "noQuestion",
"degree": "1.5"
}
]
},
{
"gId": "G0",
"id": 3,
"question_id": 36,
"question_title": "Moderately impaired social - emotional understanding. Shows an under - standing of facial expressions, tone of voice, and body language\r\nonly when these cues are exaggerated. Is likely to ignore or misunderstand or perspective of others.",
"question_note": null,
"answers": [
{
"answer_index": "1",
"answer_text": "yes",
"answer_image": null,
"answers_redirection_group_index": "G1",
"answers_redirection_group_question": "noQuestion",
"degree": "3"
},
{
"answer_index": "2",
"answer_text": "No",
"answer_image": null,
"answers_redirection_group_index": "noGroup",
"answers_redirection_group_question": "noQuestion",
"degree": 0
},
{
"answer_index": "3",
"answer_text": "N/A",
"answer_image": null,
"answers_redirection_group_index": "G1",
"answers_redirection_group_question": "noQuestion",
"degree": "2.5"
}
]
}
]
},
{
"id": "G1",
"name": "Emotional expression and regulation of emotions",
"questions": [
{
"gId": "G1",
"id": 3,
"question_id": 104,
"question_title": "Moderately abnormal emotional response. Expression of emotions is flat, excessive, or frequently inconsistent with situation or content\r\nof verbalized topic. May display greater emotion than expected about special interest or idiocy necrotic concerns. Ability to describe or\r\nunderstand emotional states in self is limited. Serious problems with emotional regulation that occur frequently in at least one setting.",
"question_note": null,
"answers": [
{
"answer_index": "1",
"answer_text": "Yes",
"answer_image": null,
"answers_redirection_group_index": "G2",
"answers_redirection_group_question": "noQuestion",
"degree": "3"
},
{
"answer_index": "2",
"answer_text": "No",
"answer_image": null,
"answers_redirection_group_index": "noGroup",
"answers_redirection_group_question": "noQuestion",
"degree": 0
},
{
"answer_index": "3",
"answer_text": "N/A",
"answer_image": null,
"answers_redirection_group_index": "G2",
"answers_redirection_group_question": "noQuestion",
"degree": "2.5"
}
]
},
{
"gId": "G1",
"id": 2,
"question_id": 103,
"question_title": "Mildly abnormal emotional response. Emotional expressions are relatively flat, distorted, or slightly exaggerated. Nonverbal expression of\r\nemotions does not always match verbal content. Able to describe several emotions in self but limited compared to developmental level.\r\nMay have intermittent emotional regulation problems.",
"question_note": null,
"answers": [
{
"answer_index": "1",
"answer_text": "Yes",
"answer_image": null,
"answers_redirection_group_index": "G2",
"answers_redirection_group_question": "noQuestion",
"degree": "2"
},
{
"answer_index": "2",
"answer_text": "No",
"answer_image": null,
"answers_redirection_group_index": "noGroup",
"answers_redirection_group_question": "noQuestion",
"degree": 0
},
{
"answer_index": "3",
"answer_text": "N/A",
"answer_image": null,
"answers_redirection_group_index": "G2",
"answers_redirection_group_question": "noQuestion",
"degree": "1.5"
}
]
}
]
}

]


How can I create array from all "question_id" ? so I can return this questions from question table by using " whereIn('id', $ids)->get(); "


so the neede array must be like $ids = [34,35,36,104,103] so I will use this array to get this questions from question table



My Answer:
You can achieve this by looping through the array of objects and extracting the "question_id" values into a new array. Here's how you can do it:

php
// Assuming $data contains the array of objects
$ids = [];

foreach ($data as $item) {
foreach ($item['questions'] as $question) {
$ids[] = $question['question_id'];
}
}

// Now $ids will contain the desired array of question_ids
// You can use this array to retrieve the questions from the question table
$questions = Question::whereIn('id', $ids)->get();


Make sure to replace `Question` with the actual model class name for your questions table. This code snippet will extract all the "question_id" values from the array of objects and store them in the `$ids` array, which you can then use to retrieve the corresponding questions from the database.

Rate this post

4 of 5 based on 6676 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us