How To Generate RSS Feed In Laravel 9

Websolutionstuff | Dec-12-2022 | Categories : Laravel PHP

In this article, we will see how to generate an RSS feed in laravel 9. Here, we will generate an RSS feed in laravel 7, laravel 8, and laravel 9. RSS stands for Really Simple Syndication. RSS is a web feed that allows users and applications to access updates to websites in a standardized, computer-readable format.

Nowadays many websites use an RSS feed to publish frequently updated information on the internet such as blog articles, news headlines, videos, etc. RSS uses the XML file format to generate the RSS feed.

So, let's see how to create an RSS feed in laravel 9, laravel 9 RSS feed generator, and laravel 7/8/9 RSS feed generator.

Step 1: Install laravel 9

In this step, we will install laravel 9 using the following command.

composer create-project laravel/laravel laravel9_RSS_Feed

 

 

Step 2: Create Model and Migration

In this step, we will create migration and model using the following command.

php artisan make:migration create_articles_table

Migration:

<?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('articles', 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('articles');
    }
};

Now, migrate the table into the database using the following command.

php artisan migrate

After that, we will create an Article model using the following command.

php artisan make:model Article

In the Article model, we will add a column's name.

app/Models/Articles.php

<?php
  
namespace App\Models;
  
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
  
class Article extends Model
{
    use HasFactory;
  
    protected $fillable = [
        'title', 'slug', 'body'
    ];
}

 

 

Step 3: Create Dummy Records

Now, we will create dummy records using the laravel factory. we will create an Article factory class using the following command and generate dummy records using tinker.

php artisan make:factory ArticleFactory

database/factories/ArticleFactory.php

<?php
  
namespace Database\Factories;
  
use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\Article;
use Illuminate\Support\Str;
  
/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Article>
 */
class ArticleFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     *
     * @var string
     */
    protected $model = Article::class;
    /**
     * Define the model's default state.
     *
     * @return array

     */
    public function definition()
    {
        return [
            'title' => $this->faker->text(),
            'slug' => Str::slug($this->faker->text()),
            'body' => $this->faker->paragraph()
        ];
    }
}

After that, we will run the tinker command and create fake records.

php artisan tinker
    
App\Models\Article::factory()->count(50)->create();

 

Step 4: Create Routes

In this step, we will add routes in the web.php file.

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('rssfeed', [RSSFeedController::class, 'index']);

 

 

Step 5: Create Controller

Now, create a controller and add the index() function to the RSSFeedController File. So, add the below code to that file.

app/Http/Controllers/RSSFeedController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use App\Models\Article;
  
class RSSFeedController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $articles = Article::latest()->get();
  
        return response()->view('rss', [
            'articles' => $articles
        ])->header('Content-Type', 'text/xml');
    }
}

 

Step 6: Create Blade Files

In this step, we will create a rss.blade.php file and generate the RSS feed.

resources/views/rss.blade.php

<?= '<?xml version="1.0" encoding="UTF-8"?>' ?>
<rss version="2.0">
    <channel>
        <title> Websolutionstuff | Web Developing Website </title>
        <link> http://websolutionstuff.com/ </link>
        <description> The websolutionstuff website is provides different types of examples and tutorials like Laravel, PHP, JavaScript, jQuery, HTML, CSS, Bootstrap, SQL and so on. </description>                
        <language>en-us</language>
        @foreach($articles as $article)
            <item>
                <title>{{$article->title}}</title>
                <link>{{$article->slug}}</link>
                <description>{{$article->body}}</description>
                <author>Websolutionstuff</author>                
                <pubDate>{{$article->created_at->toRssString()}}</pubDate>
            </item>
        @endforeach
    </channel>
</rss>
 
Step 7: Run Laravel Application

Now, run the RSS Feed generator using the following command.

php artisan serve

Open the below URL in your browser.

http://localhost:8000/rssfeed

Output:

rss_feed_generator

 


You might also like:

Recommended Post
Featured Post
How to Get Last 15 Records in Laravel 10
How to Get Last 15 Records in...

Welcome, fellow developers! In this guide, I'll walk you through the straightforward process of fetching the latest...

Read More

Dec-08-2023

How to Integrate Cashfree Payment Gateway in Laravel 10
How to Integrate Cashfree Paym...

Hello developers! Today, we're about to embark on a journey to elevate our Laravel applications by integrating...

Read More

Feb-12-2024

How to Load Iframe in jQuery onload Event
How to Load Iframe in jQuery o...

Hello, laravel web developers! In this article, we'll see how to load an iframe in jQuery on a load event. In jQuery...

Read More

Jun-21-2024

How To Import CSV File In MySQL Using Node.js
How To Import CSV File In MySQ...

In this tutorial we will see how to import CSV file in MySQL using Node.js. Import and export CSV/EXCEL file in Nod...

Read More

Jul-30-2021