方法是在 docs Using a custom directory structure下。
PathGenerator
要覆盖默认文件夹结构,可以path_generator
在配置文件中指定符合 -interface 的类。
例如,您可以创建一个扩展该接口的新类并为您的模型返回一些路径
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/';
}
}
然后更新配置文件并指向该类:
'path_generator' => CustomPathGenerator::class,
参考:
spatie/laravel-medialibrary Docs使用自定义目录结构。
Laracasts Spatie MediaLibrary 默认存储取决于模型“最佳答案”。