PHP
Laravel
How To Generate RSS Feed In Laravel?
Today, I will let you know example of laravel generate rss feed. let’s discuss about laravel get rss feed. you can see laravel create rss feed. we will help you to give example of rss feed in laravel. you can use this example with laravel 5, laravel 6, laravel 7, laravel 8 and laravel 9 versions. RSS stands for Really Simple Syndication and A RSS Feed is an XML File that has a list of updated URLs of your website. You can use the RSS feed for automatically sending email notification updates.
- 4.5/5.0
- Last updated 05 September, 2022
- By Admin
In this example, we will create posts table with title, slug and body. Then we will create a factory for dummy posts. Then after we will generate an XML file and list all URLs for posts. It's a very basic example. so let's follow and you will get a sitemap file for your website and submit it to the webmaster's tool.
Let's follow below steps:
Step 1: Install Laravel
This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:
composer create-project laravel/laravel example-app
Step 2: Create Post Migration and Model
In this step, we will create migration and model. So let's run below command to create posts table.
php artisan make:migration create_posts_table
Next, simple update below code to migration file.
.database/migrations/create_posts_table.php
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 created new migration with below command:
php artisan migrate
Now, run below command to create Post model
php artisan make:model Post
Then update following code to Post model.
app/Models/Post.php
Step 3: Create Post Factory
php artisan make:factory PostFactory
Next, copy below code and update PostFactory.php file.
database/factories/PostFactory.php
*/ 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 */ public function definition() { return [ 'title' => $this->faker->text(), 'slug' => Str::slug($this->faker->text()), 'body' => $this->faker->paragraph() ]; } }
Then simply run tinker command and create dummy posts.
php artisan tinker App\Models\Post::factory()->count(30)->create();
Step 4: Create Route
In this step, we will create one route sitemap.xml so let's add it.
routes/web.php
Step 5: Create Controller
In this step, we have 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
get(); return response()->view('rss', [ 'posts' => $posts ])->header('Content-Type', 'text/xml'); } } ?>
Step 6: Create View File
In Last step, let's create rss.blade.php for display all posts and put following code:
resources/views/rss.blade.php
'.PHP_EOL ?> ?> en {{ now() }} @foreach($posts as $post)- @endforeach
title }}]]> {{ $post->slug }}body !!}]]> {{ $post->category }} {{ $post->id }} {{ $post->created_at->toRssString() }}
Run Laravel App:
All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:
php artisan serve
Now, Go to your web browser, type the given URL and view the app output:
http://localhost:8000/feed
I hope it can help you...