Setup
To get started install the package into your application by running the code below.
composer require laravelium/feed
php artisan vendor:publish --provider="Laravelium\Feed\FeedServiceProvider"
Implementation
To generate the RSS Feed, you will have to define a new route in web.php and just call it "rss-feed" which essentially is the URL user has to call to view the RSS feed.
Route::get('rss-feed', function () { /* create new feed instance */ $feed = app("feed"); });
$posts = Post::query() ->whereNotNull('published_at') ->take(20) ->get();
/* set your feed's title, description, link, pubdate and language */ $feed->title = 'Your site name here'; $feed->description = 'Your site description here'; $feed->logo = 'https://your-site-url.test/img/logo.png'; $feed->link = 'https://your-site-url.test'; $feed->setDateFormat('datetime'); // 'datetime', 'timestamp' or 'carbon' $feed->pubdate = $posts[0]->published_at; $feed->lang = 'en'; $feed->setShortening(true); // true or false $feed->setTextLimit(100); // maximum length of description text
Now since the main structure is already in place, to load the resource do loop it through and add it to the feed using "addItem" method. It will look like the following but do change it with your own data structure.
$posts->each(fn ($post) => $feed->addItem([ 'title' => $post->title, 'author' => 'Your Name', 'url' => $post->url, 'link' => $post->url, 'pubdate' => $post->published_at, 'description' => $post->summary, 'content' => $post->body ]));
$feed->ctype = "application/xml"; return $feed->render('rss');
Final Code
The final code will look like the following and once it's ready, try to view it on the browser by visiting "rss-feed" path.
Route::get('rss-feed', function () { /* create new feed */ $feed = app("feed"); $posts = Post::query() ->whereNotNull('published_at') ->take(20) ->get(); /* set your feed's title, description, link, pubdate and language */ $feed->title = 'Your site name here'; $feed->description = 'Your site description here'; $feed->logo = 'https://your-site-url.test/img/logo.png'; $feed->link = 'https://your-site-url.test'; $feed->setDateFormat('datetime'); /* 'datetime', 'timestamp' or 'carbon' */ $feed->pubdate = $posts[0]->published_at; $feed->lang = 'en'; $feed->setShortening(true); /* true or false */ $feed->setTextLimit(100); /* maximum length of description text */ $posts->each(fn ($post) => $feed->addItem([ 'title' => $post->title, 'author' => 'Your Name', 'url' => $post->url, 'link' => $post->url, 'pubdate' => $post->published_at, 'description' => $post->summary, 'content' => $post->body ])); $feed->ctype = "application/xml"; return $feed->render('rss'); });