Easiest Way to Allow CORS in Laravel 6 or Any Version

Last modified on January 21, 2020 43 sec read

In this tutorial, I’m going to share how to allow Cross-Origin Resource Sharing (CORS) in Laravel 6 or any version of Laravel.

According to Wikipedia: Cross-origin resource sharing (CORS) is a mechanism that allows restricted resources on a web page to be requested from another domain outside the domain from which the first resource was served.

We can allow CORS in many ways in Laravel. Let’s solve the issue by following the easiest way.

The Easiest Solution

Go to bootstrap folder and open app.php file. Then just add these lines at the top of the file.

header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: *');
header('Access-Control-Allow-Headers: *');

Another Solution

Run this artisan command:

php artisan make:middleware Cors

Now open Cors.php from App\Http\Middleware folder and replace handle() function with this code:

public function handle($request, Closure $next)
    return $next($request)
        ->header('Access-Control-Allow-Origin', '*')
        ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, PATCH, DELETE, OPTIONS')
        ->header('Access-Control-Allow-Headers', 'Content-Type, Authorizations');

Lastly, open Kernel.php from App\Http folder add the below line to the $middleware array:

protected $middleware = [

Now run the application and call API from anywhere.

The tutorial is over. Thank you. 🙂

Monthly Newsletter

One email a month, packed with the latest tutorials, delivered straight to your inbox.
We'll never send any spam or promotional emails.

Hey, I'm Md Obydullah. I build open-source projects and write article on Laravel, Linux server, modern JavaScript and more on web development.