Cut PHP Code Review Time & Bugs into Half with CodeRabbit
Or try paginated search →
Published on by Paul Redmond
Creating an RSS feed in Laravel isn’t the most challenging task, but using a package and a few tips can help you create an RSS feed relatively quick.
We are going to use the spatie/laravel-feed package to walk through going from a brand new Laravel 5.6 project to serving RSS feeds.
The first thing we need to do is create a new Laravel project. You can use the laravel new installer command, or use composer:
Next, let’s install the spatie/laravel-feed package and copy the configuration file into the project:
We are going to create some test model data to set up our RSS feed for a collection of Events. Think of this model as an RSS feed for upcoming events for a church or a meetup group. The model name Event is superfluous, but that’s the best name I could come up with, so we’ll go with it!
First up, let’s create the model for our RSS feed along with a migration file and a factory file:
In the migration class, let’s define the generic, simple schema:
Next up, open the database/factories/EventFactory.php file and add the following:
There might be a better way to generate an HTML description, but I am assuming that the UI for creating an event would provide a WYSIWYG field for writing the description.
We can use our schema and factory to seed some test data in order to finish setting up our RSS feed:
In the created EventsSeeder class, generate some seed events we can use:
Next, add the EventsSeeder to the DatabaseSeeder class:
Depending on how you created the files, you might need to run composer dump-autoload to pick up the seeder files. You can then run migrations (assuming you’ve set up a database following the documentation) to create the tables and populate the database with some seed data:
And just like that, we have some fake events to work with!
Using Spatie’s package, we implement an interface to support the spatie/laravel-feed package configuration with our AppEvent model.
We will have two methods for using our Event model with a feed:
First up, here’s the implementation for the toFeedItem() method with our schema in the app/Event.php file:
At the moment, this method won’t work quite right, because we don’t have a link attribute but other than that, this method is pretty straightforward.
We are converting an individual model instance to a FeedItem instance to generate the final RSS format.
Next, we’ll add the getFeedItems() static method to the Event.php file:
We are merely returning all results as a collection that is converted to individual FeedItem instances.
The last method we’ll add to the model is an accessor for the $event->link attribute:
Using implicit route model binding, we are generating the link property on the model that will be used to link to individual RSS items.
The last piece we need to work on is creating a controller and a route so the route('events.show', $this) will work as expected.
First, let’s generate a controller:
Inside the EventsController create a show() method that returns the model which results in a JSON response. We are not going to flesh out the EventsController@show method and return HTML in this tutorial; I’ll leave that up to you:
Next, we need to configure the main RSS feed route in the published config/feed.php file we generated earlier:
Last up, we need to add the RSS route and the EventsController@show route to the bottom of routes/web.php:
The Route::feeds() method is a route macro that defines the necessary route(s) from our config/feed.php configuration. We use the {event} route parameter and take advantage of implicit route model binding that we used earlier to generate the event link from within the model.
Our feed should be available at /events.rss now!
You can also add an RSS link to the default welcome page by adding @include('feed::links') in the <head> of the view:
If you visit the homepage of the project, you’ll get something like the following link tag:
With hardly any effort, we created a working RSS feed for our model data, and you can imagine how little you’d have to do to add RSS to existing model data in your applications!
Check out the laravel-feed GitHub page for the source code and package setup.
I wrote about Route macros yesterday in Laravel Route Tips to Improve Routing, and the Laravel feed package has an excellent route macro defined in the FeedServiceProvider that you should check out.
Paul Redmond
Staff writer at Laravel News. Full stack web developer and author.
Join 40k+ other developers and never miss out on new tips, tutorials, and more.
Explore hundreds of open positions today.
The ultimate guide to auditing, testing, fixing and improving your Laravel applications so you can build better apps faster and with more confidence.
World class Laravel experts with GenAI dev skills. LATAM-based, embedded engineers that ship fast, communicate clearly, and elevate your product. No bloat, no BS.
Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $2500/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!
Easily create and manage your servers and deploy your Laravel applications in seconds.
The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.
CodeRabbit is an AI-powered code review tool that specializes in PHP and Laravel, running PHPStan and offering automated PR analysis, security checks, and custom review features while remaining free for open-source projects.
Connect with experienced developers in a friendly, noise-free environment. Get insights, share ideas, and find support for your coding challenges. Join us today and elevate your Laravel skills!
Providing innovation and stability to ensure your web application succeeds.
Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.
Get Lucky Now – the ideal choice for Laravel Development, with over a decade of experience!
E-Commerce for Laravel. An open-source package that brings the power of modern headless e-commerce functionality to Laravel.
The official Laravel job board
SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.
Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.
and follow us on
© 2012 – 2025 Laravel News
A division of dotdev inc.
Colophon / About






