Step 1: create a route create a route for this in your routes/web.php file Ex.
Route::get('sitemap.xml', [App\Http\Controllers\SitemapController::class,'index'])->name('sitemapxml');
Step 2: Create Controller
Now you can create SitemapController.php with artisan command -->
php artisan make:controller SitemapController
Now you can put this code in your controller
Use App\Models\Post; //your model name
public function index() { $page = Post::where('status', '=', 1)->get();
return response()->view('sitemap_xml', ['page' => $page])->header('Content-Type', 'text/xml'); }
Step 3: Create View
Now please create a view file in
resources/view/sitemap_xml.blade.php file with this code
Put this code in that created view file
<?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"> @foreach ($page as $post) <url> <loc>{{ url($post->page_slug) }}</loc> <lastmod>{{ $post->updated_at->tz('UTC')->toAtomString() }}</lastmod> <priority>0.9</priority> </url> @endforeach </urlset>
We are Recommending you:
- How to change timezone in laravel 8
- Laravel 8/7 Overwriting the Default Pagination System
- Laravel 8 .htaccess file for php 8
- Integrate Zoho SMTP Mail Configurations in Laravel?
- Custom 404 Page In Laravel 8
- Why Use the Repository Pattern in a Laravel Application
- Laravel's .htaccess to remove "public" from URL
- How to use soft delete in Laravel?
- Laravel remove public from url
Step Out of Your Comfort Zone: 10 Powerful...
Is Mobile Reels Harming Our Children? Here's...
Simple body language tricks1. Stand with...
Best Free Websites to Learn CodingIf you...
we provide custom social share links for...
1. What is Git, and why is it important?Git...
ParameterDescriptionto Required. Specifies...
Convert a PEM File to PPK | Easy Guide for...
When We talk about Linux commands, what we...