How to generate dynamic sitemap in Laravel

How to generate dynamic sitemap in Laravel

Published at - Aug 15, 2021


I usually use the following package for all of my sites to generate the sitemap.xml file automatically:

The setup goes something like this:

  • First install the package via composer:
composer require spatie/laravel-sitemap
  • Configuration

If you would like to change the default options, you could publish the config by using the following command:

php artisan vendor:publish --provider="Spatie\Sitemap\SitemapServiceProvider" --tag=config

This will copy the config to config/sitemap.php where you would be able to change it depending on your needs.

  • Usage

To generate a sitemap for your website with all of the found links, you can use the following:

  • Automation

To automate the process what you could do is create an artisan command and then add it to your scheduler.

To do that just run the following:

php artisan make:command GenerateSitemap

And then update the content of the app/Console/Commands/GenerateSitemap.php file accordingly. I would usually use something like this:


namespace App\Console\Commands;

use Illuminate\Console\Command;
use Spatie\Sitemap\SitemapGenerator;

class GenerateSitemap extends Command
     * The console command name.
     * @var string
    protected $signature = 'sitemap:generate';

     * The console command description.
     * @var string
    protected $description = 'Generate the sitemap.';

     * Execute the console command.
     * @return mixed
    public function handle()
        // modify this to your own needs

Then to generate your sitemap just run:

php artisan sitemap:generate

To schedule this to run on daily basis just add the following to your app/Console/Kernel.php file:

protected function schedule(Schedule $schedule)

That is pretty much it, then your sitemap.xml should be available at example.xml/sitemap.xml.

For more information I would recommend going through the official repo here:

Hope that this helps!

About author

Harendra Kanojiya

Hello, I am Harendra Kumar Kanojiya - Owner of this website and a Fullstack web developer. I have expertise in full-stack web development using Angular, PHP, Node JS, Python, Laravel, Codeigniter and, Other web technologies. I also love to write blogs on the latest web technology to keep me and others updated. Thank you for reading the articles.

Related Posts -

Laravel real-time chat application
Laravel real-time chat appl ...

Laravel Realtime ChatBuild Status Code Climate A realtime chat sample w...

Selenium for Laravel
Selenium for Laravel ...

Table of Contents Key Points:Requirements:Installation guide:Create fir...

How to deploy my own laravel 8 project to ubuntu with nginx
How to deploy my own larave ...

LaraSail is a CLI tool for Laravel to help you Sail the Servers of the Digi...

Laravel backup &#8211; A modern backup solution for Laravel apps
Laravel backup &#8211; A mo ...

This Laravel package creates a backup of your application. The backup ...

A mail driver to quickly preview mail
A mail driver to quickly pr ...

Table of Contents InstallationConfiguring the mail transportRegisterin...

Follow Us

Follow us on facebook Click Here

Facebook QR
Scan from mobile
Join our telegram channel Click Here
Telegram QR
Scan from mobile