From aca5b15abacf6ae428d68ac2d8f00160c91700cf Mon Sep 17 00:00:00 2001 From: lukinovec Date: Thu, 1 Sep 2022 18:05:38 +0200 Subject: [PATCH] Add spatie/laravel-medialibrary integration guide (#194) --- source/docs/v3/integrations/spatie.blade.md | 26 +++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/source/docs/v3/integrations/spatie.blade.md b/source/docs/v3/integrations/spatie.blade.md index 6507559..a468d1c 100644 --- a/source/docs/v3/integrations/spatie.blade.md +++ b/source/docs/v3/integrations/spatie.blade.md @@ -42,3 +42,29 @@ Event::listen(TenancyEnded::class, function (TenancyEnded $event) { ``` The reason for this is that spatie/laravel-permission caches permissions & roles to save DB queries, which means that we need to separate the permission cache by tenant. + +## **laravel-medialibrary** {#laravel-medialibrary} + +To generate the correct media URLs for tenants, create a custom URL generator class extending `Spatie\MediaLibrary\Support\UrlGenerator\DefaultUrlGenerator` and override the `getUrl()` method: + +```php +use Spatie\MediaLibrary\Support\UrlGenerator\DefaultUrlGenerator; + +class TenantAwareUrlGenerator extends DefaultUrlGenerator +{ + public function getUrl(): string + { + $url = asset($this->getPathRelativeToRoot()); + + $url = $this->versionUrl($url); + + return $url; + } +} +``` + +Then, change the `url_generator` in the media-library config to the custom one (make sure to import it): + +```php +'url_generator' => TenantAwareUrlGenerator::class, +```