Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
1.9 kB
1
Indexable
Never
<?php

namespace App\Http\Controllers;

use App\Models\clients;
use App\Models\url;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ProxyController extends Controller
{
    public function getClientDataTree($id) {
        try {
            // Retrieve client details from the database
            $client_details_url = DB::table('urls')->where('owner', $id)->pluck('url');
            $client_details_email = DB::table('emails')->where('client', $id)->pluck('email');
    
            // Combine the retrieved details into a 2-dimensional array
            $client_details = [$client_details_email, $client_details_url];
    
            // Initialize an empty array to hold the structured data
            $structured_data = [];
    
            // Loop through each type of client detail (email and URL)
            for ($i = 0; $i < count($client_details); $i++) {
                // Initialize an empty array to hold details of this type
                $details_array = [];
    
                // Loop through each detail of this type
                foreach ($client_details[$i] as $detail) {
                    // Push the detail into the array
                    $details_array[] = $detail;
                }
    
                // Assign the array of details to the corresponding key
                if ($i == 0) {
                    $structured_data['emails'] = $details_array;
                } else {
                    $structured_data['urls'] = $details_array;
                }
            }
    
            // Return the structured data
            return response()->json(['success' => $structured_data]);
        } catch (\Throwable $th) {
            // Return error message if an exception occurs
            return response()->json(['error' => $th->getMessage()], 500);
        }
    }
    
}
Leave a Comment