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:
-
Ensure
APP_URLis Correctly Set: Make sure that your.envfile has the correctAPP_URLset tohttps.APP_URL=https://staging.contenthead.com -
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'); } } -
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.
-
Check Trusted Proxies: If you're behind a load balancer or proxy, you might need to configure trusted proxies in your
TrustProxiesmiddleware. 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; } -
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 -
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.