"messages": [
{
"id": 1,
"customer": "Dr. Kane Hill",
"customer_id": 1,
"created_at": "2024-11-19 11:18:57",
"text": "Hello, how are you?"
},
{
"id": 2,
"customer": "Dr. Kane Hill",
"customer_id": 1,
"created_at": "2024-11-19 11:20:57",
"text": "I am good, thanks! How about you?"
},
{
"id": 3,
"customer": "Prof. Samir McClure III",
"customer_id": 2,
"created_at": "2024-11-19 11:21:57",
"text": "Hey, what time is it?"
},
{
"id": 4,
"customer": "Prof. Samir McClure III",
"customer_id": 2,
"created_at": "2024-11-19 11:22:57",
"text": "It is 3 PM."
},
{
"id": 5,
"customer": "Shad Leffler",
"customer_id": 3,
"created_at": "2024-11-19 11:23:57",
"text": "Did you finish the project?"
},
{
"id": 6,
"customer": "Shad Leffler",
"customer_id": 3,
"created_at": "2024-11-19 11:24:57",
"text": "New mesh"
},
{
"id": 7,
"customer": "Prof. Samir McClure III",
"customer_id": 2,
"created_at": "2024-11-19 11:26:57",
"text": "Not bed?"
},
{
"id": 8,
"customer": "Prof. Samir McClure III",
"customer_id": 2,
"created_at": "2024-11-19 11:27:57",
"text": "Cool?"
}
]
"messages2": [
{
"customer": "Dr. Kane Hill",
"messages_id": 1,
"customer_id": 1,
"messages": [
{
"id": 1,
"created_at": "2024-11-19 11:18:57",
"text": "Hello, how are you?",
},
{
"id": 2,
"created_at": "2024-11-19 11:20:57",
"text": "I am good, thanks! How about you?",
}
]
},
{
"customer": "Prof. Samir McClure III",
"messages_id": 2,
"customer_id": 2,
"messages": [
{
"id": 3,
"created_at": "2024-11-19 11:21:57",
"text": "Hey, what time is it?",
},
{
"id": 4,
"created_at": "2024-11-19 11:22:57",
"text": "It is 3 PM."
}
]
},
{
"customer": "Shad Leffler",
"messages_id": 3,
"customer_id": 3,
"messages": [
{
"id": 4,
"created_at": "2024-11-19 11:23:57",
"text": "Did you finish the project?"
},
{
"id": 5,
"created_at": "2024-11-19 11:24:57",
"text": "New mesh"
}
]
},
{
"customer": "Prof. Samir McClure III",
"messages_id": 4,
"customer_id": 2,
"messages": [
{
"id": 6,
"created_at": "2024-11-19 11:26:57",
"text": "Not bed?"
},
{
"id": 7,
"created_at": "2024-11-19 11:27:57",
"text": "Cool?"
},
]
},
]
public function toArray(Request $request): array
{
$messages = $this->whenLoaded('messages')->sortBy('id');
$messagesGroupedByCustomer = [];
$messagesIdCounter = 1;
$currentCustomerId = null;
$currentCustomerMessages = [];
foreach ($messages as $message) {
if ($currentCustomerId !== $message['customer_id']) {
if ($currentCustomerId !== null) {
$messagesGroupedByCustomer[] = [
'customer' => $currentCustomerMessages[0]['customer']->full_name,
'messages' => array_map(function ($msg) {
return [
'id' => $msg['id'],
'created_at' => $msg['created_at']->toDateTimeString(),
'text' => $msg['text'],
];
}, $currentCustomerMessages),
];
$messagesIdCounter++;
}
$currentCustomerId = $message['customer_id'];
$currentCustomerMessages = [];
}
$currentCustomerMessages[] = $message;
}
if ($currentCustomerId !== null) {
$messagesGroupedByCustomer[] = [
'customer' => $currentCustomerMessages[0]['customer']->full_name,
'messages' => array_map(function ($msg) {
return [
'id' => $msg['id'],
'created_at' => $msg['created_at']->toDateTimeString(),
'text' => $msg['text'],
];
}, $currentCustomerMessages),
];
}
$data['date'] = "Сегодня";
$data['item'] = $messagesGroupedByCustomer;
return $data;
}
<?php
error_reporting(-1);
ini_set('display_errors', 1);
$json_data = <<<JSON
{"messages": [
{
"id": 1,
"customer": "Dr. Kane Hill",
"customer_id": 1,
"created_at": "2024-11-19 11:18:57",
"text": "Hello, how are you?"
},
{
"id": 2,
"customer": "Dr. Kane Hill",
"customer_id": 1,
"created_at": "2024-11-19 11:20:57",
"text": "I am good, thanks! How about you?"
},
{
"id": 3,
"customer": "Prof. Samir McClure III",
"customer_id": 2,
"created_at": "2024-11-19 11:21:57",
"text": "Hey, what time is it?"
},
{
"id": 4,
"customer": "Prof. Samir McClure III",
"customer_id": 2,
"created_at": "2024-11-19 11:22:57",
"text": "It is 3 PM."
},
{
"id": 5,
"customer": "Shad Leffler",
"customer_id": 3,
"created_at": "2024-11-19 11:23:57",
"text": "Did you finish the project?"
},
{
"id": 6,
"customer": "Shad Leffler",
"customer_id": 3,
"created_at": "2024-11-19 11:24:57",
"text": "New mesh"
},
{
"id": 7,
"customer": "Prof. Samir McClure III",
"customer_id": 2,
"created_at": "2024-11-19 11:26:57",
"text": "Not bed?"
},
{
"id": 8,
"customer": "Prof. Samir McClure III",
"customer_id": 2,
"created_at": "2024-11-19 11:27:57",
"text": "Cool?"
}
]}
JSON;
$messages = json_decode($json_data, true)['messages'];
$messages2 = [];
$messageCounter = 0;
foreach($messages as $row) {
$key = $row['customer_id'];
if(!isset($messages2[$key])) {
$messages2[$key] = [
'customer' => $row['customer'],
'message_id' => (++$messageCounter),
'customer_id' => $row['customer_id'],
'messages' => [],
];
}
$messages2[$key]['messages'][] = [
'id' => $row['id'],
'created_at' => $row['created_at'],
'text' => $row['text'],
];
}
$messages2 = array_values($messages2);
echo json_encode($messages2, JSON_PRETTY_PRINT);