Skip to content

Commit

Permalink
optimize the relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
MoamenEltouny authored Mar 13, 2022
1 parent 34dcc01 commit ba4f07c
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/HasFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
trait HasFiles
{
use HasCustomAttributes;

/**
* Files Atrributes on Save/Create
*
Expand Down Expand Up @@ -149,7 +150,13 @@ public function isFileAttribute(string $key): bool
*/
public function files()
{
return $this->morphMany(File::class, 'model');
$hasThumbnails = false;

foreach ($this->filesOptions ?? [] as $options)
if (isset($options['thumbnail']))
$hasThumbnails = true;

return $this->morphMany(File::class, 'model')->with($hasThumbnails ? 'file.thumbnail' : 'file');
}

/**
Expand Down

0 comments on commit ba4f07c

Please sign in to comment.