Something like this?
// Get items as array
$items = json_decode(file_get_contents(storage_path() . "/file.json"), true);
//Filter collection
collect($items)->filter(function ($item) { return $item['price'] <= 0;})
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Dear all ,
I have a json file that I need to query and show following staff:
Count the total number of orders? Count the number of orders that were FREE? Count the number of orders that were placed in GBP?
I have loaded the file in my laravel app but I have no idea how to query the file.
this is the first row of the JSON file
{
"created_at": "2016-01-17T17:02:25.660Z",
"description": "Consectetur est deserunt laborum excepteur amet consectetur aute anim magna excepteur quis veniam eu quis. Anim ullamco est commodo duis aliqua commodo commodo commodo. Ullamco do officia veniam veniam exercitation veniam enim adipisicing. Exercitation fugiat occaecat mollit sit magna duis est cillum qui id pariatur incididunt adipisicing velit. Adipisicing pariatur non commodo dolor do sunt commodo. Duis magna irure excepteur eu exercitation deserunt adipisicing ut ipsum consectetur fugiat labore eu. Culpa non veniam cillum et reprehenderit ad reprehenderit veniam qui.",
"title": "fugiat magna Lorem aliquip qui",
"customer":
{
"billing_address":
{
"postcode": "MO73 2ID",
"county": "Westmorland",
"city": "Oley",
"street": "124 Veranda Place"
},
"shipping_address":
{
"postcode": "MO73 2ID",
"county": "Westmorland",
"city": "Oley",
"street": "124 Veranda Place"
},
"phone": "+447482939767",
"email": "[email protected]",
"name":
{
"last": "Rios",
"first": "Ingrid"
}
},
"currency": "USD",
"price": "0.33",
"url": "https://example.com/products/5889b0a6797714883c501c23",
"index": 13,
"uuid": "30906bb3-ff12-4517-a9ea-71bb2ed79c0e",
"id": "5889b0a6797714883c501c23"
},
any idea on how to deal with this ?
@doncho85 Pretty sure that was just an example, so you can do stuff yourself. And you never mentioned anything about sum?
Anyways. Here is an example of each
$all = collect($items);
$allCount = $all->count();
$allSum = $all->sum('price');
$free = $all->filter(function ($item) {
return $item['price'] == 0;
});
$freeCount = $free->count();
$freeSum = $free->sum('price');
$gbp = $all->filter(function ($item) {
return $item['currency'] == 'GBP';
});
$gbpCount = $free->count();
$gbpSum = $free->sum('price');
Please or to participate in this conversation.