diff --git a/app/Models/Article.php b/app/Models/Article.php index dbd01bc25..90f0585f6 100644 --- a/app/Models/Article.php +++ b/app/Models/Article.php @@ -12,10 +12,13 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Collection; use Illuminate\Support\Str; use Laravel\Scout\Searchable; +use Spatie\Feed\Feedable; +use Spatie\Feed\FeedItem; -final class Article extends Model +final class Article extends Model implements Feedable { use HasFactory; use HasAuthor; @@ -28,6 +31,8 @@ final class Article extends Model const TABLE = 'articles'; + const FEED_PAGE_SIZE = 20; + /** * @inheritdoc */ @@ -319,4 +324,21 @@ public static function nextForSharing(): ?self ->orderBy('submitted_at', 'asc') ->first(); } + + public static function getFeedItems(): Collection + { + return self::paginate(self::FEED_PAGE_SIZE) + ->getCollection(); + } + + public function toFeedItem(): FeedItem + { + return FeedItem::create() + ->id($this->id()) + ->title($this->title()) + ->summary($this->excerpt()) + ->updated($this->updatedAt()) + ->link(route('articles', $this->slug())) + ->authorName($this->author()->name()); + } } diff --git a/config/feed.php b/config/feed.php index aa9ef8f5a..d55272dda 100644 --- a/config/feed.php +++ b/config/feed.php @@ -1,5 +1,6 @@ '', ], + + 'articles' => [ + /* + * Here you can specify which class and method will return + * the items that should appear in the feed. For example: + * [App\Model::class, 'getAllFeedItems'] + * + * You can also pass an argument to that method. Note that their key must be the name of the parameter: + * [App\Model::class, 'getAllFeedItems', 'parameterName' => 'argument'] + */ + 'items' => [Article::class, 'getFeedItems'], + + /* + * The feed will be available on this url. + */ + 'url' => '/articles/feed', + + 'title' => 'Laravel.io Articles RSS Feed', + 'description' => 'The RSS feed for Laravel.io articles contains a list of all articles posted by community members.', + 'language' => 'en-US', + + /* + * The image to display for the feed. For Atom feeds, this is displayed as + * a banner/logo; for RSS and JSON feeds, it's displayed as an icon. + * An empty value omits the image attribute from the feed. + */ + 'image' => '', + + /* + * The format of the feed. Acceptable values are 'rss', 'atom', or 'json'. + */ + 'format' => 'atom', + + /* + * The view that will render the feed. + */ + 'view' => 'feed::atom', + + /* + * The mime type to be used in the tag. Set to an empty string to automatically + * determine the correct value. + */ + 'type' => '', + + /* + * The content type for the feed response. Set to an empty string to automatically + * determine the correct value. + */ + 'contentType' => '', + ], ], ]; diff --git a/resources/views/articles/overview.blade.php b/resources/views/articles/overview.blade.php index fbfbf9529..c23362ef0 100644 --- a/resources/views/articles/overview.blade.php +++ b/resources/views/articles/overview.blade.php @@ -183,6 +183,13 @@
+ + diff --git a/resources/views/forum/overview.blade.php b/resources/views/forum/overview.blade.php index f53e71e5a..aaf9f54d1 100644 --- a/resources/views/forum/overview.blade.php +++ b/resources/views/forum/overview.blade.php @@ -164,7 +164,7 @@