get(); for ($i = 0; $i <= 24; $i++) { foreach ($galleries as $gallery) { //Get file size for that hour for that gallery $start = $day." ".$i.":00:00"; $end = $day." ".$i.":59:59"; $sql = "SELECT SUM(size) AS size FROM `go-mysql-admin`.images WHERE gallery = ".$gallery->id." AND uploaded_at < \"".$start."\" AND (deleted_at > \"".$end."\" OR deleted_at IS NULL);"; $size = DB::select($sql)[0]->size; $split = explode("-", $day); $storage = \App\Models\Storage::query() ->where("gallery", "=", $gallery->id) ->where("tenant", "=", $gallery->tenant) ->where("date", "=", $day) ->where("hour", "=", $i) ->first(); if(is_null($storage)) { $storage = new \App\Models\Storage(); $storage->date = $day; $storage->year = $split[0]; $storage->month = $split[1]; $storage->day = $split[2]; $storage->hour = $i; $storage->gallery = $gallery->id; $storage->tenant = $gallery->tenant; } if($storage->size != $size) { $storage->size = $size; $storage->saveOrFail(); } } } } }