Skip to content

Commit

Permalink
Get url of the image for last media
Browse files Browse the repository at this point in the history
  • Loading branch information
rrakibul committed Oct 5, 2023
1 parent e5251db commit b0ac628
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/InteractsWithMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,13 @@ public function getFirstMedia(string $collectionName = 'default', $filters = [])
return $media->first();
}

public function getLastMedia(string $collectionName = 'default', $filters = []): ?Media
{
$media = $this->getMedia($collectionName, $filters);

return $media->last();
}

/*
* Get the url of the image for the given conversionName
* for first media for the given collectionName.
Expand All @@ -293,6 +300,23 @@ public function getFirstMediaUrl(string $collectionName = 'default', string $con
{
$media = $this->getFirstMedia($collectionName);

return $this->handleMediaUrl($media, $collectionName, $conversionName);
}

/*
* Get the url of the image for the given conversionName
* for last media for the given collectionName.
* If no profile is given, return the source's url.
*/
public function getLastMediaUrl(string $collectionName = 'default', string $conversionName = ''): string
{
$media = $this->getLastMedia($collectionName);

return $this->handleMediaUrl($media, $collectionName, $conversionName);
}

private function handleMediaUrl(?Media $media, string $collectionName = 'default', string $conversionName = ''): string
{
if (! $media) {
return $this->getFallbackMediaUrl($collectionName, $conversionName) ?: '';
}
Expand Down

0 comments on commit b0ac628

Please sign in to comment.