How to force Laravel to generate HTTPS instead of HTTP for the route, URL, and asset functions.

Laravel

Your website domain starts with ‘https://’ but when you generate a URL inside your Laravel application it starts with ‘http://’ and you find the browsers give you that your connection is not secure alert.

{{ url('post')}}

Result http:\\ your_website_ domain \post.

but what you need is https:// your_website_ domain/post.

Here is the solution, let’s make sure we have the APP_URL starts with https in .env file

APP_URL=https:// your_website_ domain/

Open config\app.php and change the following code.

 'url' => env('APP_URL', 'https://your_domain'),

Finally open app\Providers\AppServiceProvider.php files add the following code to force Laravel to use https via URL::forceSchema(‘https’) and set the

Finally open app\Providers\AppServiceProvider.php files add the following code to force Laravel to use https via URL::forceSchema(‘https’) and set the https server param to true in the boot method.

//
use Illuminate\Support\Facades\URL;
//
//
    public function boot()
    {
       //
       URL::forceSchema('https')

       $this->app['request']->server->set('HTTPS', true);
    }

You only need to force SSL in production, not in the local env that works under http.

I hope I could help thanks.