Browser blocks your Laravel API with a CORS error
Access to fetch at 'https://api.example.com/api/orders' from origin 'https://app.example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.Short answer
Laravel answers CORS preflights itself through its HandleCors middleware, driven by config/cors.php. The browser still blocks you when the route path is not listed in paths, when credentials are on but supports_credentials is false, or when the session cookie domain does not cover the front end's subdomain. Publish the file, edit it, and test with curl.
Fix it fast
1Find out which cause you have
Send the preflight yourself against the exact URL the page calls. If the response has no Access-Control headers, the path is outside the CORS configuration; if it has them, the problem is credentials or cookies.
curl -i -X OPTIONS https://api.example.com/api/orders \ -H "Origin: https://app.example.com" \ -H "Access-Control-Request-Method: POST"2Match what you see, then jump to its fix
- 1
No Access-Control-* headers on the OPTIONS response for a URL that does not start with api/The URL path is not covered by the paths optionFix - 2
The browser says credentials mode is include; the preflight response lacks Access-Control-Allow-CredentialsThe front end sends cookies but supports_credentials is falseFix - 3
CORS headers are correct, yet login works but the next request is unauthenticated (401 or 419)The session cookie is not valid for the SPA's subdomainFix
- 1
What does the error look like?
Access to fetch at 'https://api.example.com/api/orders' from origin 'https://app.example.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.Response to preflight request doesn't pass access control check
Cause 1 of 3
The URL path is not covered by the paths option
1Check
The stock cors config only applies to api/* and sanctum/csrf-cookie. Routes in routes/web.php, a custom prefix, or an API prefix you changed in bootstrap/app.php do not match. In Laravel 11 and newer the config file is not in a fresh project; the framework uses built-in defaults.
php artisan route:list --path=orders
ls config/cors.php2Fix
Publish the file, then add every path pattern your front end calls.
php artisan config:publish cors
// config/cors.php
'paths' => ['api/*', 'sanctum/csrf-cookie', 'webhooks/*'],
'allowed_origins' => ['https://app.example.com'],3Verify
Repeat the preflight; Access-Control-Allow-Origin must now appear.
curl -si -X OPTIONS https://api.example.com/api/orders \
-H "Origin: https://app.example.com" -H "Access-Control-Request-Method: POST" | grep -i access-controlConfirmed by raw.githubusercontent.com
Cause 2 of 3
The front end sends cookies but supports_credentials is false
1Check
In the stock file supports_credentials is false and allowed_origins is a wildcard. With credentials the browser accepts only an exact origin plus the credentials header; a wildcard is rejected.
grep -n "supports_credentials\|allowed_origins" config/cors.php2Fix
Turn credentials on and list exact origins, and enable credentials on the HTTP client too.
// config/cors.php
'allowed_origins' => ['https://app.example.com'],
'supports_credentials' => true,
// resources/js/bootstrap.js (axios)
axios.defaults.withCredentials = true;
axios.defaults.withXSRFToken = true;3Verify
The response carries Access-Control-Allow-Credentials: true and the exact origin.
curl -si https://api.example.com/sanctum/csrf-cookie \
-H "Origin: https://app.example.com" | grep -i access-controlConfirmed by laravel.com
Cause 3 of 3
The session cookie is not valid for the SPA's subdomain
1Check
Sanctum's SPA mode needs the API and the SPA on the same top-level domain. The cookie domain in config/session.php decides which subdomains receive the cookie.
grep -n "'domain'" config/session.php2Fix
Prefix the session domain with a dot so every subdomain of the root domain is covered, and list the SPA's host as stateful.
// config/session.php
'domain' => '.example.com',
// .env
SANCTUM_STATEFUL_DOMAINS=app.example.com3Verify
Log in from the SPA and call an authenticated route; the browser should send the session cookie.
php artisan config:clearConfirmed by laravel.com
How do I stop it coming back?
- Keep the allowed origins in your .env file and read them in config/cors.php, one list per environment.
- Never leave allowed_origins as a wildcard on an API that also uses cookies.
- Test the preflight with curl after each deploy, not only in the browser.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- Spotless LaundryAn on-demand laundry app: Flutter on a Laravel back end, with Firebase for live updates.
- EZ EstimaterConstruction quoting and job tracking for contractors, on a Laravel and Firebase stack.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web apps
The browser blocks your API response: no Access-Control-Allow-Origin header
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.example.com/orders. (Reason: CORS header 'Access-Control-Allow-Origin' missing).
The browser received the response but refused to hand it to your script, because it did not carry an Access-Control-Allow-Origin header naming the page's origin.
4 causes, each with a fixBack end
Laravel CORS settings have no effect: missing config/cors.php, paths and cache
Response to preflight request doesn't pass access control check
Laravel answers CORS preflights itself through the HandleCors middleware, but only for URLs listed in the paths option of config/cors.php.
4 causes, each with a fixBack end
Composer says the gd extension is missing on a Laravel project
composer install or composer update stops on a package that requires ext-gd
Composer refuses to install a package that requires ext-gd when the PHP binary running Composer has no GD extension.
4 causes, each with a fixBack end
composer require maatwebsite/excel fails to install
Composer says the package needs an older PHP than the one installed
Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.
3 causes, each with a fixBack end
composer install says the lock file is not compatible and asks for update
Your lock file does not contain a compatible set of packages. Please run composer update.
composer install checked the locked packages against this machine's PHP and extensions and found a conflict.
3 causes, each with a fixBack end
composer require laravel/horizon fails: ext-pcntl is missing
laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system
Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.
3 causes, each with a fix