{"flag":true,"single":true,"pageTitle":"Laravel xml sitemap generate dynamically for large no of records","post":{"id":101,"user_id":"1","slug":"laravel-xml-sitemap-generate-dynamically-for-large-no-of-records-e58f","title":"Laravel xml sitemap generate dynamically for large no of records","body":"<p>We'll use the spatie\/laravel-sitemap package to make this task easier. Here's a step-by-step guide:<\/p>\r\n<p><strong>1. Install the package using terminal or cmd<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>composer require spatie\/laravel-sitemap<\/code><\/pre>\r\n<p><strong>2.Create a Sitemap Controller:<\/strong><br>Run the following command to generate a new SitemapController:<\/p>\r\n<pre class=\"language-markup\"><code>php artisan make:controller SitemapController<\/code><\/pre>\r\n<p><strong>3.Modify the Sitemap Controller:<\/strong><br>Open the newly created SitemapController.php file, which is located in the app\/Http\/Controllers directory. Add the necessary imports and modify the controller as follows:<\/p>\r\n<pre class=\"language-markup\"><code>&lt;?php\r\nnamespace App\\Http\\Controllers;\r\nuse Illuminate\\Http\\Request;\r\nuse Spatie\\Sitemap\\Sitemap;\r\nuse Spatie\\Sitemap\\Tags\\Url;\r\nuse Spatie\\Sitemap\\SitemapIndex;\r\nuse Spatie\\Sitemap\\Tags\\Sitemap as SitemapTag;\r\nclass SitemapController extends Controller\r\n{\r\n   public function index(){\r\n        $sitemapIndex = SitemapIndex::create();\r\n        $sitemapIndex-&gt;add(SitemapTag::create(url('sitemap\/realestate.xml'))); \/\/u can add more here\r\n        return $sitemapIndex-&gt;toResponse(request());\r\n    }\r\n    public function realestate()\r\n    {\r\n        $realEstateCount = \\App\\Models\\RealEstate::count();\r\n        $sitemapFiles = ceil($realEstateCount \/ 3); \/\/3 per page\r\n        $sitemapIndex = SitemapIndex::create();\r\n    \r\n        for ($i = 1; $i &lt;= $sitemapFiles; $i++) {\r\n            $sitemapIndex-&gt;add(SitemapTag::create(url(\"sitemap\/realestate\/{$i}.xml\")));\r\n        } \r\n        return $sitemapIndex-&gt;toResponse(request());\r\n    }\r\n    public function realestateSitemap($page) \/\/for singlepage\r\n    {\r\n        $limit = 3;\r\n        $offset = ($page - 1) * $limit;\r\n        $realEstates = \\App\\Models\\RealEstate::offset($offset)-&gt;limit($limit)-&gt;get();\r\n        $sitemap = Sitemap::create();\r\n        foreach ($realEstates as $realEstate) {\r\n            $url = url(\"realestate\/details\/{$realEstate-&gt;id}\");\r\n            $sitemap-&gt;add(Url::create($url));\r\n        }\r\n        return $sitemap-&gt;toResponse(request());\r\n    }\r\n}<\/code><\/pre>\r\n<p><strong>4.Update the routes:<\/strong><br>Modify the routes\/web.php file to add routes for the sitemap index and each sitemap file:<\/p>\r\n<pre class=\"language-markup\"><code>Route::controller(\\App\\Http\\Controllers\\SitemapController::class)-&gt;group( function () {\r\n    Route::get('\/sitemap.xml', 'index');\r\n    Route::get('\/sitemap\/realestate.xml', 'realestate');\r\n    Route::get('\/sitemap\/realestate\/{page}.xml', 'realestateSitemap');\r\n});<\/code><\/pre>\r\n<p><strong>OUTPUT:<\/strong><\/p>\r\n<p><strong>http:\/\/127.0.0.1:8000\/sitemap.xml<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>&lt;sitemapindex xmlns=\"http:\/\/www.sitemaps.org\/schemas\/sitemap\/0.9\"&gt;\r\n&lt;sitemap&gt;\r\n&lt;loc&gt;http:\/\/127.0.0.1:8000\/sitemap\/realestate.xml&lt;\/loc&gt;\r\n&lt;lastmod&gt;2023-04-04T06:08:13+00:00&lt;\/lastmod&gt;\r\n&lt;\/sitemap&gt;\r\n&lt;\/sitemapindex&gt;<\/code><\/pre>\r\n<p><strong>http:\/\/127.0.0.1:8000\/sitemap\/realestate.xml<\/strong><\/p>\r\n<pre class=\"language-markup\"><code>&lt;sitemapindex xmlns=\"http:\/\/www.sitemaps.org\/schemas\/sitemap\/0.9\"&gt;\r\n&lt;sitemap&gt;\r\n&lt;loc&gt;http:\/\/127.0.0.1:8000\/sitemap\/realestate\/1.xml&lt;\/loc&gt;\r\n&lt;lastmod&gt;2023-04-04T05:42:30+00:00&lt;\/lastmod&gt;\r\n&lt;\/sitemap&gt;\r\n&lt;sitemap&gt;\r\n&lt;loc&gt;http:\/\/127.0.0.1:8000\/sitemap\/realestate\/2.xml&lt;\/loc&gt;\r\n&lt;lastmod&gt;2023-04-04T05:42:30+00:00&lt;\/lastmod&gt;\r\n&lt;\/sitemap&gt;\r\n&lt;\/sitemapindex&gt;<\/code><\/pre>\r\n<p>http:\/\/127.0.0.1:8000\/sitemap\/realestate\/2.xml<\/p>\r\n<pre class=\"language-markup\"><code>&lt;urlset xmlns=\"http:\/\/www.sitemaps.org\/schemas\/sitemap\/0.9\" xmlns:xhtml=\"http:\/\/www.w3.org\/1999\/xhtml\"&gt;\r\n&lt;url&gt;\r\n&lt;loc&gt;http:\/\/127.0.0.1:8000\/realestate\/details\/4&lt;\/loc&gt;\r\n&lt;lastmod&gt;2023-04-04T05:44:05+00:00&lt;\/lastmod&gt;\r\n&lt;changefreq&gt;daily&lt;\/changefreq&gt;\r\n&lt;priority&gt;0.8&lt;\/priority&gt;\r\n&lt;\/url&gt;\r\n&lt;url&gt;\r\n&lt;loc&gt;http:\/\/127.0.0.1:8000\/realestate\/details\/5&lt;\/loc&gt;\r\n&lt;lastmod&gt;2023-04-04T05:44:05+00:00&lt;\/lastmod&gt;\r\n&lt;changefreq&gt;daily&lt;\/changefreq&gt;\r\n&lt;priority&gt;0.8&lt;\/priority&gt;\r\n&lt;\/url&gt;\r\n&lt;url&gt;\r\n&lt;loc&gt;http:\/\/127.0.0.1:8000\/realestate\/details\/6&lt;\/loc&gt;\r\n&lt;lastmod&gt;2023-04-04T05:44:05+00:00&lt;\/lastmod&gt;\r\n&lt;changefreq&gt;daily&lt;\/changefreq&gt;\r\n&lt;priority&gt;0.8&lt;\/priority&gt;\r\n&lt;\/url&gt;\r\n&lt;\/urlset&gt;<\/code><\/pre>","category_id":"2","is_private":"0","created_at":"2023-04-04T00:46:27.000000Z","updated_at":"2023-04-04T01:09:55.000000Z","category":{"id":2,"user_id":"1","name":"Laravel Core","slug":"laravel-nhyt","parent_id":"1","created_at":"2023-03-14T03:58:27.000000Z","updated_at":"2023-03-20T11:30:50.000000Z"},"user":{"id":1,"name":"R GONDAL","email":"rizikmw@gmail.com","email_verified_at":null,"two_factor_confirmed_at":null,"current_team_id":"1","profile_photo_path":null,"created_at":"2023-03-12T10:49:33.000000Z","updated_at":"2025-01-10T12:59:00.000000Z","profile_photo_url":"https:\/\/ui-avatars.com\/api\/?name=R+G&color=7F9CF5&background=EBF4FF"}},"pageDesc":"We'll use the spatie\/laravel-sitemap package to make this task easier. Here's a step-by-step guide: 1. Install the package using terminal or - Laravel xml sitemap generate dynamically for large no of records (Updated: April 4, 2023) - Read more about Laravel xml sitemap generate dynamically for large no of records at my programming site [SITE]","categories":[]}