Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

truecajun's avatar

Spatie MediaLibrary not appending collection name

Today I woke up and saw that my uploaded media (picture) is not appearing correctly. I use spatie/medialibrary. I use echo Auth::user()->getFirstMediaUrl('profile-avatar'); and it returns domain/uuid/... instead of domain/profile-avatar/uuid/..

I overrided the Spatie\MediaLibrary\Support\PathGenerator\PathGenerator;, and also Spatie\MediaLibrary\Support\UrlGenerator\DefaultUrlGenerator; because I use stancl/tenancy package and I want to make all the files tenant-aware.

If I add the collection name (profile-avatar) manually after domain/ then it works perfectly.

Is this an issue with getUrl() method? Where should I check?

Regards.

0 likes
6 replies
truecajun's avatar

I've just found out that on the DefaultUrlGenerator overriding class, the getUrl() method, if I manually add profile-avatar in this case, then it works perfectly: it returns profile-avatar before the uuid. But the thing is that, shouldn't it be automatically, depending on the collection name itself? I have 2 other storage links.

Snapey's avatar

I overrided the Spatie\MediaLibrary\Support\PathGenerator\PathGenerator;

not by changing the vendor code I hope?

truecajun's avatar

@Snapey Yes, I had to. But I think I know where the problem could be. As I already stated, I had to use tenant-aware paths to not expose central domain; though if I use the default url_generator which is \Spatie\MediaLibrary\Support\UrlGenerator\DefaultUrlGenerator::class,, it works, but the path would be central-domain/profile-avatar/...

Basing on the stancl/tenancy document (tenancyforlaravel/docs/v3/integrations/spatie), one must override the getUrl() method, however, on the original method:

$url = $this->getDisk()->url($this->getPathRelativeToRoot());

takes in consideration the disk (name), which is 'profile-avatar' and successfully appends it, but, on the stancl/tenancy page:

$url = asset($this->getPathRelativeToRoot());

This one here takes in consideration the tenant-aware assets, but does not append the disk name.

Now I've just got to figure out to get the disk name $this->getDisk() and append it.

truecajun's avatar
truecajun
OP
Best Answer
Level 1

I've fixed it!

On the getUrl() method, the $url variable should be:

$url = asset($this->getDiskName().'/'.$this->getPathRelativeToRoot());

I have added $this->getDiskName().'/'.

And it works well for both (or any) storages.

I don't know what's happening, for real. One day I wake up and suddenly, most unexpected problems appear.

Snapey's avatar

NEVER edit code in the vendor folder or one day you wake up and the package has been updated and your code changes are blown away leaving no evidence of what you did previously (its not in your repo)

truecajun's avatar

@Snapey I did not edit the vendor code. That was misunderstanding. I meant to express that I've edited vendor code, by overriding the classes. Anyhow that wasn't merely the cause of the main problem. Really thanks for your contribution.

Please or to participate in this conversation.