Laravel: Rate-Limit API Routes with Throttle
Protect your Laravel API from abuse with the built-in throttle middleware — limit requests per minute per user or IP.
Generate a sitemap.xml from your database in Laravel — always up to date, no file to maintain by hand.
27 lines · Updated Nov 26, 2026
<?php
// routes/web.php
use App\Models\Post;
use Illuminate\Support\Facades\Route;
Route::get('/sitemap.xml', function () {
$posts = Post::where('status', 'published')
->latest('updated_at')
->get(['slug', 'updated_at']);
$xml = view('sitemap', compact('posts'))->render();
return response($xml, 200)
->header('Content-Type', 'application/xml');
});
// resources/views/sitemap.blade.php
// <?xml version="1.0" encoding="UTF-8"?>
// <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
// @foreach ($posts as $post)
// <url>
// <loc>{{ url('/blog/' . $post->slug) }}</loc>
// <lastmod>{{ $post->updated_at->toDateString() }}</lastmod>
// </url>
// @endforeach
// </urlset>
A sitemap tells Google and Bing which pages exist on your site and when they last changed, which helps new content get discovered and indexed faster. A static sitemap.xml goes out of date the moment you publish a new post. This snippet generates the sitemap dynamically from your database, so every new post, product or page appears automatically.
/sitemap.xml./sitemap.xml in your browser to check the output.Sitemap: https://yoursite.com/sitemap.xml to your robots.txt.&.Do I need priority and changefreq tags? Google ignores them; an accurate lastmod is what matters.
Related: Deploy Laravel on a VPS · Log 404 errors
Add the route to routes/web.php and save the Blade view as resources/views/sitemap.blade.php. Change the model and URL to match your content. Cache the response for an hour on large sites.
Share this snippet
Protect your Laravel API from abuse with the built-in throttle middleware — limit requests per minute per user or IP.
Send WhatsApp messages from Laravel through a provider API — order updates, OTPs and alerts in a few lines.
Add one search scope to any Eloquent model and search across several columns with a single method call.