Skip to content

Instantly share code, notes, and snippets.

@EmmArro
Created November 5, 2024 09:56
Custom path generation
<?php
namespace App\Services\MediaLibrary;
use Illuminate\Support\Str;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\MediaLibrary\Support\PathGenerator\PathGenerator;
class CustomPathGenerator implements PathGenerator
{
public function getPathForConversions(Media $media): string
{
return $this->getPath($media) . 'conversions/';
}
public function getPath(Media $media): string
{
return $this->getModelName($media->model_type) . '/' . $media->created_at->format('Y') . $media->created_at->format('m') . '/' . $media->id . '/';
}
public function getPathForResponsiveImages(Media $media): string
{
return $this->getPath($media) . 'responsive-images/';
}
function getModelName($namespace): string
{
return Str::afterLast($namespace, '\\');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment