Skip to content

Commit 574e2db

Browse files
Refactor delete loops to queries
To delete an object it's not necessary to load it into memory. And by doing one query instead of a delete query for every object, it's much faster.
1 parent 3e2a1f5 commit 574e2db

File tree

1 file changed

+3
-9
lines changed

1 file changed

+3
-9
lines changed

src/Models/LogToDbCreateObject.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function removeOldestIfMoreThan(int $max)
135135
$current = $this->count();
136136
if ($current > $max) {
137137
$keepers = $this->orderBy('unix_time', 'DESC')->take($max)->pluck($this->primaryKey)->toArray();
138-
if ($this->whereNotIn($this->primaryKey, $keepers)->get()->each->delete()) {
138+
if ($this->whereNotIn($this->primaryKey, $keepers)->delete()) {
139139
return true;
140140
}
141141
}
@@ -163,14 +163,8 @@ public function removeOlderThen(string $datetime)
163163
public function removeOlderThan(string $datetime)
164164
{
165165
$unixtime = strtotime($datetime);
166-
$deletes = $this->where('unix_time', '<=', $unixtime)->get();
166+
$count = $this->where('unix_time', '<=', $unixtime)->delete();
167167

168-
if (!$deletes->isEmpty()) {
169-
if ($deletes->each->delete()) {
170-
return true;
171-
}
172-
}
173-
174-
return false;
168+
return $count > 0;
175169
}
176170
}

0 commit comments

Comments
 (0)