In this tutorial, we'll go over how to create RSS feeds in Laravel 9 using the roumen/feed package. Therefore, to create an RSS feed, we are utilizing the roumen/feed package.

We may quickly deliver a fresh update of our websites using the RSS feed. such as newsletters and updates to new articles, etc.

Although Google Feedburner, Mailchimp, and Mailgun all offer RSS feed functionality, you can also create RSS feeds using Laravel.

Table of Contents

  1. Install Laravel
  2. Setting Database Configuration
  3. Create Table using migration
  4. Install roumen/feed Package
  5. Add providers and aliases
  6. Create Route
  7. Create a Model and Controller
  8. Run Our Laravel Application

Install Laravel

We're going to install Laravel 9, so first, open the terminal or command prompt and use it to navigate to the XAMPP htdocs folder directory. then execute the command below.

composer create-project --prefer-dist laravel/laravel laravel9_rss_feed

Setting Database Configuration

when Laravel has been fully installed. We must configure the database. We will now open the .env file and make the necessary changes to the database name, username, and password. Changes to a .env file can be seen below.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Enter_Your_Database_Name(laravel9_rss_feed)
DB_USERNAME=Enter_Your_Database_Username(root)
DB_PASSWORD=Enter_Your_Database_Password(root)

Create Table using migration

We must now start a migration. Consequently, we will use the command below to create a posts table migration.

php artisan make:migration create_posts_table --create=posts

following successful migration. The database/migrations/create posts table file needs to be modified as shown below.

<?php
 
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
 
class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) { 
     $table->bigIncrements('id');
            $table->string('title');
            $table->string('slug');
            $table->string('author');
            $table->text('description');
            $table->text('content');
            $table->timestamps();
 
        });
    }
 
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}
?>

Once the above file has been modified, run the command below.

php artisan migrate

Install roumen/feed Package

Now, using the command below, we'll install the roumen/feed package.

composer require roumen/feed

Add providers and aliases

In the "config/app.php" section, we will add the providers and aliases listed below.


'providers' => [
 ....
 Roumen\Feed\FeedServiceProvider::class,
],
'aliases' => [
 ....
 'Feed' => Roumen\Feed\Feed::class,
]

Create Route

In the "routes/web.php" file, add the following route code.


<?php
/*
|--------------------------------------------------------------------------
| 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('rss-feed' 'FeedController@rssFeed');
?>

Create a Model and Controller

The commands listed below assist in creating the controller and model.

php artisan make:model Post
php artisan make:controller FeedController

app/Post.php

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
    //
 protected $fillable = [
        'title','slug', 'author','description','content'
    ];
}
?>

app/Http/Controllers/FeedController.php

<?php
         
namespace App\Http\Controllers;
          
use App;
use App\Post;
use Illuminate\Http\Request;
        
class FeedController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function rssFeed(Request $request)
    {
       /* create new feed */
	   $feed = App::make("feed");
	   /* Take out 15 posts from database to create feed */
	   $posts = Post::orderBy('created_at', 'desc')->take(15)->get();
	   /* Set feed's title, description, link, publish date and language */
	   $feed->title = 'Feedtitle';
	   $feed->description = 'Feed description';
	   $feed->logo = 'logo url';
	   $feed->link = url('feed');
	   $feed->setDateFormat('datetime');
	   $feed->pubdate = $posts[0]->created_at;
	   $feed->lang = 'en';
	   $feed->setShortening(true);
	   $feed->setTextLimit(100);
	   foreach ($posts as $post)
	   {
		   $feed->add($post->title, $post->author, URL::to($post->slug), $post->created_at, $post->description, $post->content);
	   }
	   return $feed->render('atom');
	   
    }
}

?>

Run Our Laravel Application

The command listed below can be used to launch the server and run this example.

php artisan serve
http://127.0.0.1:8000/rss-feed

Recommended Posts

View All

Laravel 9 Autocomplete Search using Typeahead JS Tutorial


Laravel 8 autocomplete search using Typehead JS from MySQL database example

Laravel 9 Ajax Form Validation Example


In Laravel 9, we may ajax submit a form following validation. Laravel's ajax form validation is simple.

Laravel 8 Install Bootstrap Example Tutorial


laravel ui, laravel ui bootstrap 4, bootstrap 4, laravel 8 install bootstrap 4, install bootstrap 4 in laravel 8, how to use bootstrap in laravel 8

Laravel 9 Send SMS Notification to Mobile Phone Tutorial


Learn how to use the Vonage (nexmo) package in a Laravel application to send an SMS notice to a mobile phone.

Laravel where clause with date_format() example


laravel db raw where, laravel where date_format, laravel date_format mysql where, laravel date_format example, laravel where with month