Below my code as per documentation
use Spatie\Sitemap\Sitemap;
use Spatie\Sitemap\Tags\Url;
$sitemap = Sitemap::create()
->add(Url::create(config('app.url')))
Posts::where(['published'=>true])->each(function (Posts $post) use ($sitemap) {
$sitemap->add(Url::create(config('app.url')."/post/{$post->category}/{$post->title_slug}")->addImage($post->thumbnail,$post->post_title));
});
$sitemap->writeToFile(public_path('sitemap.xml'));
Sitemap generated is
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url>
<loc>http://127.0.0.1:8000</loc>
<lastmod>2022-10-20T16:13:52+00:00</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
<url>
<loc>http://127.0.0.1:8000/posts/hotel/tester-poster</loc>
<lastmod>2022-10-20T16:13:52+00:00</lastmod>
<changefreq>daily</changefreq>
<priority>0.8</priority>
</url>
</urlset>
And is there A way can generate in chunks I am picturing the feature and if post or pages are more than 50k generating such sitemap would take a lot of memory.