In this tutorial we are going to learn How to Create RSS Feed in Laravel 9 Example. RSS Feed makes it easier for readers to get the information they’re seeking without wasting time browsing the World Wide Web for it.
Uses of an RSS feed:
- If you submit an RSS feed then your website will gain more popularity.
- You can add your main keyword in an RSS feed that directly impacts your website ranking.
- When you update content then your web user automatically gets a notification.
- You need not submit the article manually. Your article automatically gets broadcast to your subscribers.
So, Today in this tutorial we are going to create a simple posts table with title, body and slug. Then will add dummy data to it using Factory that that will we generate a RSS Views Blade file to show our RSS.
Steps on How to Create RSS Feed in Laravel 9 Example
- Step 1: Installing Fresh New Laravel 9 Application
- Step 2: Creating Post Migration and Model
- Step 3: Creating Post Factory for Dummy Records
- Step 4: Creating Route
- Step 5: Creating Controller
- Step 6: Creating RSS Blade View File
- Step 7: Testing
- Step 8: Conclusion
Also Read: How to get Country City Address from IP Address Laravel 9
Step 1: Installing Fresh New Laravel 9 Application
First, We are going to install fresh new Laravel 9 Application. To install Laravel Application run the following command in terminal.
composer create-project laravel/laravel rss-feed-app
cd rss-feed-app
Note: “rss-feed-app” is the Laravel 9 Application name.

Step 2: Creating Post Migration and Model
First create a database in phpmyadmin. Then enter the database details to .env file.

So now, its time to create migration and model for our application. So, Let’s run the command to create a Post table.
php artisan make:migration create_posts_table
You can find the Post table under database/migrations/ update the post table code with the following code:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('slug');
$table->text('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
};

Then run the migration command:
php artisan migrate
Now, Run the following command to create Post Model.
php artisan make:model Post
Update the following code with Post model you can find it in app/Models/Post.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
protected $fillable = [
'title', 'slug', 'body'
];
}

Also Read: Automatically Backup Database Laravel 9
Step 3: Creating Post Factory for Dummy Records
So now, We are going to create Post Factory Class to create fake dummy data using tinker command. To create Post Factory run the following command in terminal.
php artisan make:factory PostFactory
Now open PostFactory.php inside database/factories/ folder and update the following code:
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\Post;
use Illuminate\Support\Str;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Post>
*/
class PostFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Post::class;
/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'title' => $this->faker->text(),
'slug' => Str::slug($this->faker->text()),
'body' => $this->faker->paragraph()
];
}
}

Now simply run the following tinker command to create a dummy posts.
php artisan tinker
App\Models\Post::factory()->count(30)->create();

Also Read: Upload Images to Server PHP Tutorial (2022)
Step 4: Creating Route
Now we are going to create a one route. Just open the routes/web.php and enter the following route.
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\RSSFeedController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::get('feed', [RSSFeedController::class, 'index']);
Step 5: Creating Controller
Now next step is to create new controller as RSSFeedController with index(). we will get all posts and pass to blade file. we will return response as xml file. so let’s update follow code:
app/Http/Controllers/RSSFeedController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class RSSFeedController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$posts = Post::latest()->get();
return response()->view('rss', [
'posts' => $posts
])->header('Content-Type', 'text/xml');
}
}

Also Read: Create Livewire CRUD Application in Laravel 9 Example
Step 6: Creating RSS Blade View File
Last Step, We are going to create a RSS Blade View File. Create a new rss.blade.php file and enter the following code:
resources/views/rss.blade.php
<?=
'<?xml version="1.0" encoding="UTF-8"?>'.PHP_EOL
?>
<rss version="2.0">
<channel>
<title><![CDATA[ LaravelTuts.com ]]></title>
<link><![CDATA[ https://laraveltuts.com/feed ]]></link>
<description><![CDATA[ Learn Laravel with LaravelTuts.com ]]></description>
<language>en</language>
<pubDate>{{ now() }}</pubDate>
@foreach($posts as $post)
<item>
<title><![CDATA[{{ $post->title }}]]></title>
<link>{{ $post->slug }}</link>
<description><![CDATA[{!! $post->body !!}]]></description>
<category>{{ $post->category }}</category>
<author><![CDATA[Hardk Savani]]></author>
<guid>{{ $post->id }}</guid>
<pubDate>{{ $post->created_at->toRssString() }}</pubDate>
</item>
@endforeach
</channel>
</rss>
Step 7: Testing
Now run the server for testing the application. Run the following command to start the Laravel server.
php artisan serve
and open the following URL in any browser.
http://localhost:8000/feed
Preview:

Step 8: Conclusion
Today, We had learn How to Create RSS Feed in Laravel 9 Example. Hope this tutorial helped you with learning Laravel 9. If you have any question you can ask us at comment section below. If you like the tutorial please subscribe our YouTube Channel and follow us on social network Facebook and Instagram.
[…] Also Read: How to Create RSS Feed in Laravel 9 Example […]
[…] Also Read: How to Create RSS Feed in Laravel 9 Example […]