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

richardhulbert's avatar

Spatie Media Library http / https issue.

I am using Media library to serve up images to a gallery.

   public function index(Request $request) {
        $page_length = $request->page_length ?? 10;

        $items = MediaStore::search($request->search, 'created_at', 'DESC', ['name', 'description'])->where('type', $request->type)->with(['owner' => function ($query) {
            $query->select('id', 'first_name', 'last_name', 'email');
        }])->paginate($page_length);
        // now lets mutate each by adding media to it
        foreach ($items->items() as $item) {
            $media = $item->getMedia('*');
            if (count($media) > 0) {
                $item->url = $media[0]->getUrl();

                if ($item->type === 0) {

                    $item->thumb = $media[0]->getUrl('thumb');
                    $item->srcset = $media[0]->getSrcset();
                }
            }

        }

        return $items;
    }

as you can see I pre fetch the urls using getUrl(); and getSrcset(); for some reason this returns paths with http instead of https.

{
id: 5,
name: "Misty lea",
description: "River lee misty morning",
type: 0,
user_id: 1,
deleted_at: null,
created_at: "2024-09-17T08:26:22.000000Z",
updated_at: "2024-09-17T08:26:22.000000Z",
url: "http://staging.contenthead.com/storage/media/5/IMG_0086.png",
thumb: "http://staging.contenthead.com/storage/media/5/conversions/IMG_0086-thumb.jpg",
srcset: "http://staging.contenthead.com/storage/media/5/responsive-images/IMG_0086___media_library_original_1280_960.png 1280w, http://staging.contenthead.com/storage/media/5/responsive-images/IMG_0086___media_library_original_1070_803.png 1070w, 
ETC...
",
owner: {},
media: []
},

my env on forge is set to APP_URL="https://staging.contenthead.com" So I am not sure where it is getting the http address. I have flushed the cache including the routes cache.

Just for sanity I ssh'd into the server and used Tinker to run: MediaStore::find(5)->getMedia('image')[0]->getUrl() which return the correct url

Any ideas?

0 likes
2 replies
LaryAI's avatar
Level 58

It looks like you're encountering an issue where the URLs generated by Spatie Media Library are using http instead of https, despite your APP_URL being set to https. This can happen due to a few reasons, such as incorrect configuration or the request not being recognized as secure.

Here are a few steps to troubleshoot and resolve this issue:

  1. Ensure APP_URL is Correctly Set: Make sure that your .env file has the correct APP_URL set to https.

    APP_URL=https://staging.contenthead.com
    
  2. Force HTTPS in the Application: You can force HTTPS in your application by adding the following code in the AppServiceProvider:

    use Illuminate\Support\Facades\URL;
    
    public function boot()
    {
        if (config('app.env') !== 'local') {
            URL::forceScheme('https');
        }
    }
    
  3. Check the Web Server Configuration: Ensure that your web server (Nginx, Apache, etc.) is correctly configured to handle HTTPS requests and forward them to your Laravel application.

  4. Check Trusted Proxies: If you're behind a load balancer or proxy, you might need to configure trusted proxies in your TrustProxies middleware. This ensures that Laravel correctly identifies the request as secure.

    namespace App\Http\Middleware;
    
    use Illuminate\Http\Middleware\TrustProxies as Middleware;
    use Fideloper\Proxy\TrustProxies as ProxyMiddleware;
    
    class TrustProxies extends Middleware
    {
        protected $proxies = '*';
    
        protected $headers = Request::HEADER_X_FORWARDED_ALL;
    }
    
  5. Clear Cache: After making these changes, clear your application cache to ensure that the new settings take effect.

    php artisan config:cache
    php artisan route:cache
    php artisan view:cache
    
  6. Verify URL Generation: Finally, verify that the URLs are being generated correctly by running a test in Tinker or by checking the output in your application.

Here is your updated index method with the above considerations:

public function index(Request $request) {
    $page_length = $request->page_length ?? 10;

    $items = MediaStore::search($request->search, 'created_at', 'DESC', ['name', 'description'])
        ->where('type', $request->type)
        ->with(['owner' => function ($query) {
            $query->select('id', 'first_name', 'last_name', 'email');
        }])
        ->paginate($page_length);

    // Now let's mutate each by adding media to it
    foreach ($items->items() as $item) {
        $media = $item->getMedia('*');
        if (count($media) > 0) {
            $item->url = $media[0]->getUrl();

            if ($item->type === 0) {
                $item->thumb = $media[0]->getUrl('thumb');
                $item->srcset = $media[0]->getSrcset();
            }
        }
    }

    return $items;
}

By following these steps, you should be able to ensure that the URLs generated by Spatie Media Library use https instead of http.

richardhulbert's avatar

@LaryAI

use Illuminate\Support\Facades\URL;

public function boot()
{
    if (config('app.env') !== 'local') {
        URL::forceScheme('https');
    }
}

Seemed to do the trick

Please or to participate in this conversation.