Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sahar_mkr's avatar

eloquent

Hi everyone, I have the collection below and I want to compute the average of all uptimes with the type API group by every day, how can I do that? I fetched all data from the table first, and I want to do calculations on it with the help of eloquent, I do not want to get average at first, I mean I want to find every day average uptime of API types. is there a good way to do so? thank you all,

	Illuminate\Database\Eloquent\Collection {#1262
#items: array:3 [
0 => App\Models\ChartData {#1263
  #connection: "mysql"
  #table: "chart_data"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  +preventsLazyLoading: false
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #escapeWhenCastingToString: false
  #attributes: array:3 [
    "date" => "2023-04-16 20:29:28"
    "uptime" => "100"
    "type" => "API"
  ]
  #original: array:3 [
    "date" => "2023-04-16 20:29:28"
    "uptime" => "100"
    "type" => "API"
  ]
  #changes: []
  #casts: []
  #classCastCache: []
  #attributeCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: array:4 [
    0 => "type"
    1 => "site_name"
    2 => "uptime"
    3 => "date"
  ]
  #guarded: array:1 [
    0 => "*"
  ]
}
1 => App\Models\ChartData {#1264
  #connection: "mysql"
  #table: "chart_data"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  +preventsLazyLoading: false
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #escapeWhenCastingToString: false
  #attributes: array:3 [
    "date" => "2023-04-16 20:29:28"
    "uptime" => "100"
    "type" => "Dashboard"
  ]
  #original: array:3 [
    "date" => "2023-04-16 20:29:28"
    "uptime" => "100"
    "type" => "Dashboard"
  ]
  #changes: []
  #casts: []
  #classCastCache: []
  #attributeCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: array:4 [
    0 => "type"
    1 => "site_name"
    2 => "uptime"
    3 => "date"
  ]
  #guarded: array:1 [
    0 => "*"
  ]
}
2 => App\Models\ChartData {#1265
  #connection: "mysql"
  #table: "chart_data"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  +preventsLazyLoading: false
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #escapeWhenCastingToString: false
  #attributes: array:3 [
    "date" => "2023-04-16 20:29:28"
    "uptime" => "100"
    "type" => "Pay"
  ]
  #original: array:3 [
    "date" => "2023-04-16 20:29:28"
    "uptime" => "100"
    "type" => "Pay"
  ]
  #changes: []
  #casts: []
  #classCastCache: []
  #attributeCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: array:4 [
    0 => "type"
    1 => "site_name"
    2 => "uptime"
    3 => "date"
  ]
  #guarded: array:1 [
    0 => "*"
  ]
}

]

0 likes
2 replies
LaryAI's avatar
Level 58

To compute the average of all uptimes with the type API group by every day, you can use the groupBy() and avg() methods in Eloquent. Here's an example:

$apiUptimes = ChartData::where('type', 'API')
    ->selectRaw('DATE(date) as date, AVG(uptime) as avg_uptime')
    ->groupBy('date')
    ->get();

foreach ($apiUptimes as $apiUptime) {
    echo "On " . $apiUptime->date . ", the average uptime was " . $apiUptime->avg_uptime . "% for API type.\n";
}

This code will retrieve all the ChartData records with the type API, group them by date, and calculate the average uptime for each day. The resulting collection will contain objects with two properties: date and avg_uptime. You can then loop through the collection and display the results as desired.

sahar_mkr's avatar

@LaryAI I don't want to use chart data, think that I don't have any access to the model, I just should work with this collection.

Please or to participate in this conversation.