Skip to content

Instantly share code, notes, and snippets.

Last active Nov 15, 2018
What would you like to do?
Code snippets for Laravel RSS feed article -
use Illuminate\Support\Facades\Storage;
use App\Models\Post;
Artisan::command('generate:feed', function () {
$this->info("Generating RSS Feed");
// It is important that you sort by the latest post
$posts = Post::where('published', true)->latest()->get();
$site = [
'name' => 'YOUR SITE NAME', // Simplest Web
'url' => 'YOUR SITE URL', // Link to your rss.xml. eg.
'description' => 'YOUR SITE DESCRIPTION',
'language' => 'YOUR SITE LANGUAGE', // eg. en, en-IN, jp
'lastBuildDate' => $posts[0]->created_at->format(DateTime::RSS), // This generates the latest posts date in RSS compatible format
// Passes posts and site data into the rss.blade.view, out comes text in rss format
$rssFileContents = view('rss', compact('posts', 'site'));
// Saves the generated rss feed into a file called rss.xml in the public folder, this works only
Storage::disk('local')->put('rss.xml', $rssFileContents);
<!-- Change the title and href to your site -->
<link rel="alternate" type="application/rss+xml" title="Simplest Web" href="" />
{!! '<'.'?'.'xml version="1.0" encoding="UTF-8" ?>' !!}
<rss version="2.0" xmlns:atom="" xmlns:content="" xmlns:media="">
<title>{{ $site['name'] }}</title>
<link>{{ $site['url'] }}</link>
<description><![CDATA[{{ $site['description'] }}]]></description>
<atom:link href="{{ $site['url'] }}" rel="self" type="application/rss+xml" />
<language>{{ $site['language'] }}</language>
<lastBuildDate>{{ $site['lastBuildDate'] }}</lastBuildDate>
@foreach($posts as $post)
<title><![CDATA[{!! $post->title !!}]]></title>
<link>{{ route('', $post->slug) }}</link>
<guid isPermaLink="true">{{ route('', $post->slug) }}</guid>
<description><![CDATA[{!! $post->description !!}]]></description>
<content:encoded><![CDATA[{!! $post->content !!}]]></content:encoded>
<dc:creator xmlns:dc="">{{ $post->author }}</dc:creator>
<pubDate>{{ $post->created_at->format(DateTime::RSS) }}</pubDate>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment