By SitemapFixer Team
Updated April 2026

Laravel Sitemap: Generate One with spatie/laravel-sitemap

Validate your Laravel sitemap freeCheck My Sitemap

Laravel does not generate a sitemap automatically. The most widely used solution is the spatie/laravel-sitemap package, which provides a fluent API for building sitemaps programmatically. Install via Composer: composer require spatie/laravel-sitemap

Installation and Basic Usage

Create a controller that builds and returns the sitemap as an XML response:

// routes/web.php
Route::get("/sitemap.xml", SitemapController::class);
 
// app/Http/Controllers/SitemapController.php
use Spatie\Sitemap\Sitemap;
use Spatie\Sitemap\Tags\Url;
 
public function __invoke()
{
return Sitemap::create()
->add(Url::create("/")->setPriority(1.0))
->add(Url::create("/about")->setPriority(0.8))
->renderResponse();
}

Generating URLs from Database Models

For dynamic content like blog posts, loop over your models and add a URL for each:

$sitemap = Sitemap::create()
->add(Url::create("/"));
 
Post::published()->each(function ($post) use ($sitemap) {
$sitemap->add(
Url::create("/blog/" . $post->slug)
->setLastModificationDate($post->updated_at)
->setPriority(0.7)
);
});

Caching the Sitemap

For large sites, generating the sitemap on every request is expensive. Wrap sitemap generation in Cache::remember with a TTL of several hours. Invalidate the cache in model observers when content is published or updated so the sitemap stays fresh.

Common Laravel Sitemap Issues

APP_URL not set correctly: If APP_URL in your .env is set to localhost or a staging URL, all sitemap URLs will use the wrong base domain. Set APP_URL to your production domain in your production environment configuration.

Memory limit on large catalogs: Fetching millions of product URLs in one query will exhaust memory. Use chunk() or lazy() instead of all() when iterating over large model collections.

Validate your Laravel sitemap
Free - checks all URLs in 60 seconds
Check My Sitemap Free

Related Guides