I'm new in Php MySql and a lot of answer in this question but I still having an error in this. So what I'm trying to do is to loop my array and to create a multidimensional associative array.
I have evaluation_details, category, segment and question tables In evaluation_details, I have the the id of category, subcategory, segment and question tables
And in category I have column is_cat and its data type is tiny int the value is either 0 or 1 which means 1 if the given category is the main category and 0 if it is sub category, so I need to create an array like this.
Inside Category a multiple subcategory and each subcategory has one question. And each category can group by segment.
array:2 [
0 => array:4 [
"id" => 2
"title" => "second evaluation form"
"emp_position" => "System Architecture"
"first_segment_name" => array:2 [
0 => {
+"category_name" => array 2 [
0 => {
+"subcategory" => sub_1
+"question" => question_1
},
1 => {
+"subcategory" => sub_2
+"question" => question_2
}
]
}
1 => {
+"category_name" => array 2 [
0 => {
+"subcategory" => sub_3
+"question" => question_3
},
1 => {
+"subcategory" => sub_4
+"question" => question_4
}
]
}
]
"second_segment_name" => array:2 [
0 => {
+"category_name" => array 3 [
0 => {
+"subcategory" => sub_5
+"question" => question_5
},
1 => {
+"subcategory" => sub_6
+"question" => question_6
}
2 => {
+"subcategory" => sub_7
+"question" => question_7
}
]
}
]
]
]
And this is what I'm trying to do
public function getEvaluation($data){
$query = DB::table(
DB::raw("(
SELECT
e.id,
e.title,
p.name position,
s.name segment
FROM
evaluation_details e_d
JOIN evaluation e ON e_d.evaluation_id = e.id
JOIN segment s ON e_d.segment_id = s.id
JOIN position p ON e.position_id = p.id
WHERE e.position_id = 2
ORDER BY segment
) As `a`
")
)->get()->toArray();
$query = json_decode(json_encode($query), true);
foreach ($query as $index => $data) {
$first_array[$index]['id'] = $data['id'];
$first_array[$index]['title'] = $data['title'];
$first_array[$index]['emp_position'] = $data['position'];
$first_array[$index]['segments'] = DB::table('evaluation_details AS e_d')
->select('c.name AS category')
->join('category AS c', 'e_d.category_id', 'c.id')
->join('evaluation As e', 'e_d.evaluation_id', '=', 'e.id')
->join('segment AS s', 'e_d.segment_id', 's.id')
->where('e.position_id', '=', '2')
->get()->toArray();
}
echo '<pre>';
dd($first_array);exit;
}
* But it gives me different result I need to have another array inside category *
array:2 [
0 => array:4 [
"id" => 2
"title" => "first evaluation form"
"emp_position" => "System Architecture"
"segments" => array:2 [
0 => {#497
+"category": "test"
}
1 => {#498
+"category": "Quality & Dependibility"
}
]
]
]
I hope anyone can help me with this.