3

I am using spatie/laravel-medialibrary:8.0.

When I uploaded file, it was saved in storage/public directory.

1/filename.file_extension, 2/filename.file_extension...

I think 1, 2... are ids of files.

Is there any way to put different prefix directory according to user?

For example, I want to put like this.

public/storage/user_id/1/filename.file_extension, 

I searched about it but couldn't find exact answer,

Can anyone help me?

Thanks

apokryfos
  • 38,771
  • 9
  • 70
  • 114
  • Show us how you are storing the files. – porloscerros Ψ May 26 '20 at 16:24
  • Thanks for your reply. $post->addMediaFromRequest('image')->toMediaCollection(); I stored like this. –  May 26 '20 at 16:25
  • I think the way to go is under docs [Using a custom directory structure](https://docs.spatie.be/laravel-medialibrary/v8/advanced-usage/using-a-custom-directory-structure/#main). Take a look at this post "best answer" [Spatie MediaLibrary default storage depending on the model](https://laracasts.com/discuss/channels/laravel/spatie-medialibrary-default-storage-depending-on-the-model) – porloscerros Ψ May 26 '20 at 17:00
  • 1
    Great!, It worked well! Please post answer! –  May 26 '20 at 17:15
  • checkout his video https://youtu.be/PEhxmEu4Bow – Shailesh Ladumor Dec 30 '20 at 06:08

1 Answers1

4

The way to go it's under docs Using a custom directory structure.

To override the default folder structure, a class that conforms to the PathGenerator-interface can be specified as the path_generator in the config file.

For example, you can create a new class that extends that interface and return some path for your models

class CustomPathGenerator implements PathGenerator
{
    public function getPath(Media $media) : string
    {
        if ($media instanceof Post) {
            return 'user_id/' . $media->user_id . '/' . $media->id;
        }
        return $media->id;
    }

    public function getPathForConversions(Media $media) : string
    {
        return $this->getPath($media) . 'conversions/';
    }

    public function getPathForResponsiveImages(Media $media): string
    {
        return $this->getPath($media) . 'responsive/';
    }
}

Then update the config file and point to that class:

'path_generator' => CustomPathGenerator::class,

References:

spatie/laravel-medialibrary Docs Using a custom directory structure.

Laracasts Spatie MediaLibrary default storage depending on the model "best answer".

porloscerros Ψ
  • 4,808
  • 2
  • 11
  • 20
  • 1
    Thank you so much! –  May 26 '20 at 17:46
  • Could you take a look this link? https://stackoverflow.com/questions/62028837/spatie-laravel-medialibrary-cant-retrieven-file-from-backblaze-storage-this-dr –  May 26 '20 at 18:23
  • When you say, "update the config file and point to that class", what config file are you referring to? – TopAngler Apr 23 '23 at 17:04
  • 1
    @TopAngler I don't remember well because it's been a while since the last time I used that library. But I was referring to the library's configuration file [/config/medialibrary.php](https://github.com/spatie/laravel-medialibrary/blob/03586bc6eb4e2b15cdea91c85e6adff9e91f37f9/config/medialibrary.php#L82). If you can't find it in your project's config directory, you probably didn't publish it, then try `php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="config"` . – porloscerros Ψ Apr 23 '23 at 17:26