Debug School

rakesh kumar
rakesh kumar

Posted on

How to grouping and summing json in laravel

how to grouping the json data
How to grouping only value from json key value pair
How to summing only value from json key value pair
how to renders in blade file summing only value from json key value pair

how to grouping the json data

if in my database data stored in json format

Assuming you have a YourModel where each record has a 'socialsite' column:

[
    {"socialsite": "facebook", "value": 50},
    {"socialsite": "twitter", "value": 30},
    {"socialsite": "utube", "value": 80},
    {"socialsite": "instagram", "value": 20},
    {"socialsite": "linkedin", "value": 40},
    {"socialsite": "pinterest", "value": 10},
    // ... additional records
]
Enter fullscreen mode Exit fullscreen mode
// Retrieve data from the database and group it by the 'socialsite' column
$groupedData = YourModel::all()->groupBy('socialsite');

// Transform the grouped data to an array
$groupedArray = $groupedData->map(function ($items) {
    return $items->toArray();
})->toArray();

// Convert the grouped array to JSON
$jsonData = json_encode($groupedArray);
Enter fullscreen mode Exit fullscreen mode

output

{
    "facebook": [{"socialsite": "facebook", "value": 50}, {"socialsite": "facebook", "value": 60}],
    "twitter": [{"socialsite": "twitter", "value": 30}, {"socialsite": "twitter", "value": 30}],
    "utube": [{"socialsite": "utube", "value": 80}, {"socialsite": "utube", "value": 90}],
    "instagram": [{"socialsite": "instagram", "value": 20}],
    "linkedin": [{"socialsite": "linkedin", "value": 40}],
    "pinterest": [{"socialsite": "pinterest", "value": 10}]
    // ... additional groups
}
Enter fullscreen mode Exit fullscreen mode

how to grouping only value from json key value pair

if in my database data stored in json format

Assuming you have a YourModel where each record has a 'socialsite' column:

[
    {"socialsite": "facebook", "value": 50},
    {"socialsite": "twitter", "value": 30},
    {"socialsite": "utube", "value": 80},
    {"socialsite": "instagram", "value": 20},
    {"socialsite": "linkedin", "value": 40},
    {"socialsite": "pinterest", "value": 10},
    // ... additional records
]
Enter fullscreen mode Exit fullscreen mode
// Retrieve data from the database and group it by the 'socialsite' column
$groupedData = YourModel::all()->groupBy('socialsite');

// Transform the grouped data to a JSON-friendly format
$jsonData = $groupedData->map(function ($items) {
    return $items->pluck('value')->toArray();
})->toJson();
Enter fullscreen mode Exit fullscreen mode

output

{
    "facebook": [50, 60],
    "twitter": [30, 30],
    "utube": [80, 90],
    "instagram": [20],
    "linkedin": [40],
    "pinterest": [10]
    // ... additional groups
}
Enter fullscreen mode Exit fullscreen mode

how to summing only value from json key value pair

if in my database data stored in json format
if in my database data stored in json format

Assuming you have a YourModel where each record has a 'socialsite' column:

[
    {"socialsite": "facebook", "value": 50},
    {"socialsite": "twitter", "value": 30},
    {"socialsite": "utube", "value": 80},
    {"socialsite": "instagram", "value": 20},
    {"socialsite": "linkedin", "value": 40},
    {"socialsite": "pinterest", "value": 10},
    // ... additional records
]
Enter fullscreen mode Exit fullscreen mode
// Retrieve data from the database and group it by the 'socialsite' column
$groupedData = YourModel::all()->groupBy('socialsite');

// Calculate the sum of 'value' for each 'socialsite'
$sumData = $groupedData->map(function ($items) {
    return $items->sum('value');
})->toArray();

// Transform the result to JSON
$jsonSumData = json_encode($sumData);
Enter fullscreen mode Exit fullscreen mode

output

{
    "facebook": 110,
    "twitter": 60,
    "utube": 170,
    "instagram": 20,
    "linkedin": 40,
    "pinterest": 10
    // ... additional groups
}
Enter fullscreen mode Exit fullscreen mode

how to renders in blade file summing only value from json key value pair

public function yourControllerMethod()
{
    $groupedData = YourModel::all()->groupBy('socialsite');

    // Transform the grouped data to a JSON-friendly format
    $jsonData = $groupedData->map(function ($items) {
        return $items->pluck('value')->toArray();
    })->toJson();

    return view('your_blade_view', compact('jsonData'));
}
Enter fullscreen mode Exit fullscreen mode
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Your Blade View</title>
</head>
<body>

<div id="resultContainer">
    <!-- JSON data will be displayed here using jQuery -->
</div>

<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<script>
    $(document).ready(function () {
        // Get the JSON data passed from the controller
        var jsonData = {!! $jsonData !!};

        // Iterate over the JSON data and append to the container
        $.each(jsonData, function (socialsite, values) {
            var jsonString = socialsite + ': ' + values.join(', ');
            $("#resultContainer").append('<div>' + jsonString + '</div>');
        });
    });
</script>

</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)