diff --git a/src/Models/Behaviors/HasMedias.php b/src/Models/Behaviors/HasMedias.php index 719cd9be7..2350ab544 100644 --- a/src/Models/Behaviors/HasMedias.php +++ b/src/Models/Behaviors/HasMedias.php @@ -52,36 +52,22 @@ public function medias() ->withTimestamps()->orderBy(config('twill.mediables_table', 'twill_mediables') . '.id', 'asc'); } - private function findMedia($role, $crop = "default") + private function findMedia($role, $crop = 'default') { - $foundMedia = false; - $media = $this->medias->first(function ($media) use ($role, $crop, &$foundMedia) { + $media = $this->medias->first(function ($media) use ($role, $crop) { if (config('twill.media_library.translated_form_fields', false)) { $localeScope = $media->pivot->locale === app()->getLocale(); } - if (!$foundMedia) { - $foundMedia = $media->pivot->role === $role && ($localeScope ?? true); - } - - return $foundMedia && $media->pivot->crop === $crop; + return $media->pivot->role === $role && $media->pivot->crop === $crop && ($localeScope ?? true); }); - if (!$media && config('twill.media_library.translated_form_fields', false)) { - $media = $this->medias->first(function ($media) use ($role, $crop, &$foundMedia) { - if (!$foundMedia) { - $foundMedia = $media->pivot->role === $role; - } - - return $foundMedia && $media->pivot->crop === $crop; + if (! $media && config('twill.media_library.translated_form_fields', false)) { + $media = $this->medias->first(function ($media) use ($role, $crop) { + return $media->pivot->role === $role && $media->pivot->crop === $crop; }); } - if ($foundMedia && !$media && config('app.debug')) { - // In this case we found the media but not the crop because our result is still empty. - throw new MediaCropNotFoundException($crop); - } - return $media; }