I’m working in a Laravel 11 web application. I have a model called Application
and a hasMany
relationship to ApplicationEntry
models. I need to find the Application
, and delete the oldest entry. I’m concerned over the memory usage of the following and whether this is optimised?
**
* Handle the event.
*/
public function handle(ProcessDuplicateApplication $event): void
{
$application = Application::findOrFail($event->applicationId);
if (! $application) {
return;
}
Sleep::for(rand(0, 100))->milliseconds();
if ($application->entries()->count() > 2) {
$oldest = $application->entries()->oldest()->first();
if ($oldest) {
$oldest->delete();
}
}
}
This is the entries
relationship
/**
* Get the application entries that this model has
*/
public function entries(): HasMany
{
return $this->hasMany(ApplicationEntry::class);
}