CORS Middleware in Laravel 6

Hi Dev,

Today, i will let you know how to use cors middleware in laravel 6. we will use barryvdh/laravel-cors cors middleware in laravel 6 application. we can easily cors enable in laravel 6 using barryvdh composer package.

barryvdh package provide header configuration for cors middleware in laravel 6. you can follow some step to done with laravel 6.

One my viewer sent me a message with screen shot "Access to XMLHttpRequest at 'http://localhost:8000/api/users' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource." and he was told me when i was access my api from other project then it is show me error like this way.

However, i know the solution of he sent me message because i fetch many time this error and solved it using barryvdh/laravel-cors composer package with laravel 6. i sent him github like with barryvdh/laravel-cors package.

Than i thought i should share one tutorial with how to use cors middleware in laravel 6. so you can follow bellow tutorial and solved your error:

Create API Route

In this is step we need to create routes for testing. so open your "routes/api.php" file and add following route.


Route::get('/test', function (Request $request) {

return response()->json(['Laravel 6 CORS Example from ItSolutionStuff.com']);


User API to Another Project File

Now i am going to create new html file and try to access my api using jquery ajax. so you can see bellow file code that i written.


<!DOCTYPE html>



<title>Laravel 6 CORS Middleware Tutorial - ItSolutionStuff.com</title>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" crossorigin="anonymous"></script>



<script type="text/javascript">


type: "GET",

dataType: "json",

url: 'http://localhost:8000/api/test',

success: function(data){







Ok, now if i run this file, it's give this error:

"Access to XMLHttpRequest at 'http://localhost:8000/api/test' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource."

Now we will resolve how to solve this error using barryvdh/laravel-cors composer package.

Install barryvdh/laravel-cors

first of all we will install barryvdh/laravel-cors composer package by following composer command in your laravel 6 application.

composer require barryvdh/laravel-cors

After successfully install package, open config/app.php file and add service provider and alias.


'providers' => [




Now we need to use in middleware kernel file.

So let's change it:



protected $middleware = [





After that you can run your another project html file and check it.

It will works :)

I hope it can help you...

November 21, 2019 | Category : LaravelLaravel 6
Related Posts

Blog Search