Fluber
7 months ago

Laravel spatie/medialibrary addToCollection not working

Posted 7 months ago by Fluber

I have a post model and registered collection and conversions of images the post:

public function registerMediaCollections()
{
    $this->addMediaCollection('posts');
}

public function registerMediaConversions(Media $media = null)
{
    $this->addMediaConversion('banners')
        ->nonQueued();
    $this->addMediaConversion('covers')
        ->nonQueued();
}

When I save image in controller with:

public function store(AddEditPostRequest $request)
{
    $post = Post::create($request->except(['banner', 'cover']));
    if ($request->banner) {
        $filename = md5($request->banner) . $request->banner->getClientOriginalExtension();

        $post->addMediaFromRequest('banner')->usingFileName($filename)->toMediaCollection('shops');
    }

    if ($request->cover) {
        $filename = md5($request->cover) . $request->cover->getClientOriginalExtension();

        $post->addMediaFromRequest('cover')->usingFileName($filename)->toMediaCollection('shops');
    }

    return redirect()->route('posts.index', $post);
}

Medialibrary save images on incorrect path. I have storage folder. And he's not create new folder posts. Why? I get this structure of folder storage: enter image description here Folders: 1,2,3,5 and others.. this is folders of posts images.. How I can write this folders on one folder posts?

Please sign in or create an account to participate in this conversation.