Skip to content

Commit

Permalink
optimize the files query
Browse files Browse the repository at this point in the history
  • Loading branch information
MoamenEltouny authored Feb 24, 2022
1 parent 8e2397b commit 8173c9a
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/HasFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,17 @@ protected static function bootHasFiles()
public function _getFileAttribute($key)
{
if ($this->isFileAttribute($key)) {
$file = $this->files()->where('field', $key)->first();
if ($this->relationLoaded('files')) {
$file = $this->files;
} else {
$file = $this->files();

if (isset($this->filesOptions) && isset($this->filesOptions[$key]) && $this->filesOptions[$key]['thumbnail'])
$file = $file->with('file.thumbnail');
}

$file = $this->relationLoaded('files') ? $this->files : $this->files();
$file = $file->where('field', $key)->first();
return $file ? $file->file : null;
}
}
Expand Down Expand Up @@ -148,9 +158,6 @@ public function files()
public function clearFiles()
{
foreach ($this->files()->get() as $file) {
if ($thumb = $file->file->thumbnail)
$thumb->delete();

$file->file->delete();
}
}
Expand Down

0 comments on commit 8173c9a

Please sign in to comment.