Step 1: php artisan make:middleware Cors
Step 2: 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');
}
Step 3: Lastly, open Kernel.php from App\Http folder add the below line to the $middleware array:
protected $middleware = [
...
\App\Http\Middleware\Cors::class,
];
Now run the application and call API from anywhere.