get(); foreach ($closedChatGroups as $chatGroup) { DB::transaction(function () use ($chatGroup) { $company = get_company('id',$chatGroup->company_id); $ticket = insertTicket($chatGroup->email, $company->email, $chatGroup->subject, '','chat',$chatGroup->name,$chatGroup->user_id,'done',true ); // Fetch all messages from the chat group $messages = $chatGroup->messages; // Insert each message as a response to the ticket foreach ($messages as $message) { // Create a new Response instance $response = new Response; $html = $message->message; if($message->type == 'image'){ $html = ""; } if($message->type == 'file'){ $html = "View File"; } // Set the properties of the Response $response->message = $html; $response->ticket_id = $ticket->id; $response->user_id = $message->from == 'user'?0:1; // You may want to dynamically set the user_id based on the authenticated user $response->created_at = $message->created_at; $response->updated_at = $message->updated_at; // Save the response to the database $response->save(); } // Optionally update the chat group status to indicate a ticket has been created $chatGroup->update(['status' => 'ticket_created']); }); } $this->info('Chat groups checked and tickets created for closed statuses.'); return 0; } }