Pre-1.0. Not intended for public use yet. The API is subject to undocumented change, and these pages may already be wrong about it.

Skip to content

Composing Feeds ​

Introduction ​

Compose and render any feed-shaped data you already have: a project showcase, your talks, the steps an order went through. Storyfeed reads it into the same payload as a recorded feed, ready for the same kits, and stores nothing.

A composed feed is built in PHP with Storyfeed::compose(). To read the same kind of items from an array, a config entry or an API, use a feed source.

Composing a Feed ​

Add an entry for each item with add. Each entry takes the methods you use to record an activity:

A controller, or wherever the feed is read
php
use Storyfeed\Facades\Storyfeed;

$feed = Storyfeed::compose()
    ->add(fn ($entry) => $entry
        ->by($order->customer)
        ->action('place', $order)
        ->publishedAt($order->placed_at))
    ->add(fn ($entry) => $entry
        ->headline('Opened the doors to the new kitchen')
        ->publishedAt($openedAt))
    ->get();

get returns the same collection of items as any read. cursorPaginate and simplePaginate page a composed feed, and only, except, role filters and limit narrow it.

An entry accepts the role methods (by, on, to, in, at, and the others), action or verb, data, body, startsAt, endsAt and publishedAt. A model in a role is read through its toFeed method, so it must be Feedable.

A role also takes a party name, such as 'Storyfeed', or an entity with no model, as an array. See Defining Items for the array's keys.

The closure receives a Storyfeed\Sources\Entry. If your app has its own Entry class, import Storyfeed's under another name: use Storyfeed\Sources\Entry as FeedEntry;.

Writing Headlines ​

Give an entry its own headline with headline. Replacements work as they do for Laravel's __ helper:

A controller, or wherever the feed is read
php
$feed = Storyfeed::compose()
    ->add(fn ($entry) => $entry
        ->headline(':actor shipped :object (:version)', [
            'actor' => $team,
            'object' => $release,
            'version' => 'v0.20',
        ]))
    ->get();

A key that names a role, such as actor or object, sets that role, so the name renders linked and the actor's face sits on the rail. Any other key is replaced in the text, as :version, :Version or :VERSION. A model under a key that is not a role throws an InvalidArgumentException.

A headline that names an empty role throws too, unless the token sits in an optional segment: ':actor shipped[ to :target]' drops the segment when the entry has no target.

An entry's own headline wins over the feed file's. An entry with a verb and no headline reads the verb's headline from your feed file. The verb is optional: an entry with only a headline has no verb or icon.

Adding Many Entries ​

addMany adds an entry for each item in a collection, as Eloquent's createMany creates a model for each:

A controller, or wherever the feed is read
php
$feed = Storyfeed::compose()
    ->addMany($releases, fn (Release $release, $entry) => $entry
        ->by('Storyfeed')
        ->action('ship', $release)
        ->publishedAt($release->shipped_at))
    ->get();

Keeping Your Order ​

A composed feed reads newest first. inOrder keeps the entries in the order you added them, and its cursor pages by position:

A controller, or wherever the feed is read
php
$feed = Storyfeed::compose()
    ->addMany($projects->sortBy('position'), fn ($project, $entry) => $entry
        ->headline(':object', ['object' => $project]))
    ->inOrder()
    ->get();

A feed kept in order never groups, so reading it with live throws an InvalidArgumentException.

Dateless Entries ​

An entry without publishedAt is dateless. Its node has published_at: null, and the kits draw it with no time. Without inOrder, dateless entries follow the dated ones, in the order you added them.

Entries dated in the future are shown.

Grouping a Composed Feed ​

A composed feed reads as Log, whatever grouping.default says, so each entry keeps its own headline. Call live to group its dated entries with the same axes and burst windows as stored activities. A dateless or verbless entry is never grouped.

Composing From Feedable Models ​

A Feedable model in an entry's role gives its label from toFeed() and its link and pictures from feedMedia(), each time the composed feed is read. Your models stay the entities; the feed is composed from them. A renamed project shows its new name the next time the page loads.

app/Models/Project.php
php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedEntity;
use Storyfeed\FeedImage;
use Storyfeed\FeedMedia;

class Project extends Model implements Feedable
{
    use InteractsWithFeed;

    public function toFeed(): FeedEntity
    {
        return FeedEntity::make(label: $this->name);
    }

    public static function feedMedia(FeedContext $context): ?FeedMedia
    {
        $project = $context->model();

        return FeedMedia::make()
            ->url(route('projects.show', $context->routeKey()))
            ->image($project ? FeedImage::make()
                ->src(asset("media/{$project->photo}.jpg"))
                ->alt($project->name) : null);
    }
}
php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedEntity;
use Storyfeed\FeedImage;
use Storyfeed\FeedMedia;

class Project extends Model implements Feedable
{
    use InteractsWithFeed;

    public function toFeed(): FeedEntity
    {
        return FeedEntity::make(label: $this->name);
    }

    public static function feedMedia(FeedContext $context): ?FeedMedia
    {
        $project = $context->model();

        return FeedMedia::make(
            url: route('projects.show', $context->routeKey()),
            image: $project ? FeedImage::make(
                src: asset("media/{$project->photo}.jpg"),
                alt: $project->name,
            ) : null,
        );
    }
}

In a composed feed, $context->model() returns the instance the entry was given, saved or not, and runs no query.

A showcase has no dates. Its order is yours, such as a position column. The card's picture is the project's image:

app/Http/Controllers/HomeController.php
php
use App\Models\Project;
use Storyfeed\Body\MediaObject;
use Storyfeed\Facades\Storyfeed;

$projects = Project::orderBy('position')->get();

$showcase = Storyfeed::compose()
    ->addMany($projects, fn (Project $project, $entry) => $entry
        ->by('Hawkins Middle AV Club')
        ->headline(':object, :tagline', [
            'object' => $project,
            'tagline' => $project->tagline,
        ])
        ->body(MediaObject::make(
            subject: $project->name,
            content: $project->tagline,
            image: $project->feedMediaImage(),
        )))
    ->inOrder()
    ->get();
HC
Darkroom, the photo club's prints
Darkroom

Darkroom

the photo club's prints

HC
Paper Route, the school paper, delivered
Paper Route

Paper Route

the school paper, delivered

HC
High Score, the arcade leaderboard
High Score

High Score

the arcade leaderboard

Composing Common Feeds ​

Listing Talks ​

Upcoming talks read soonest first, then past talks most recent first. Order the two halves yourself and keep the order:

app/Http/Controllers/TalkController.php
php
use App\Models\Talk;
use Storyfeed\Facades\Storyfeed;

[$upcoming, $past] = Talk::all()
    ->partition(fn (Talk $talk) => $talk->starts_at->isFuture());

$ordered = [
    ...$upcoming->sortBy('starts_at'),
    ...$past->sortByDesc('starts_at'),
];

$talks = Storyfeed::compose()
    ->addMany($ordered, fn (Talk $talk, $entry) => $entry
        ->headline(':actor speaks at :object', [
            'actor' => $talk->speaker,
            'object' => $talk->event,
        ])
        ->publishedAt($talk->starts_at))
    ->inOrder()
    ->get();
Robin Buckley
Robin Buckley speaks at Hawkins Public Library
Robin Buckley
Robin Buckley speaks at the Bloomington Language Fair
Robin Buckley
Robin Buckley speaks at the Hawkins High assembly

Showing a Record's Progress ​

An order's page can show the steps it went through, oldest first, from the timestamps it already has. Nothing is recorded, so the steps never reach the shop's main feed:

app/Http/Controllers/OrderController.phpshow()
php
use Storyfeed\Facades\Storyfeed;

return view('orders.show', [
    'order' => $order,
    'progress' => Storyfeed::compose()
        ->add(fn ($entry) => $entry
            ->by($order->customer)
            ->action('place', $order)
            ->to($order->shop)
            ->publishedAt($order->placed_at))
        ->add(fn ($entry) => $entry
            ->by($order->confirmedBy)
            ->action('confirm', $order)
            ->publishedAt($order->confirmed_at))
        ->add(fn ($entry) => $entry
            ->by($order->confirmedBy)
            ->action('ready', $order)
            ->publishedAt($order->ready_at))
        ->inOrder()
        ->get(),
]);
resources/views/orders/show.blade.php
blade
<x-storyfeed::feed :page="$progress" />

The headlines come from the verbs in your feed file.

Erica Sinclair
Steve Harrington
Steve Harrington

Rendering a Composed Feed ​

A composed feed renders like any other. Hand it to the Blade kit:

routes/web.php
php
use App\Models\Project;
use Illuminate\Support\Facades\Route;
use Storyfeed\Facades\Storyfeed;

Route::get('/projects', fn () => view('projects', [
    'page' => Storyfeed::compose()
        ->addMany(Project::orderBy('position')->get(), fn (Project $project, $entry) => $entry
            ->by('Hawkins Middle AV Club')
            ->headline(':object', ['object' => $project]))
        ->inOrder()
        ->get(),
]));
resources/views/projects.blade.php
blade
<x-storyfeed::feed :page="$page" />

A composed feed can't answer involving(), query() or members(), which need stored history. See Source Limitations.

Running Without Tables ​

A composed feed reads none of Storyfeed's tables. An app that only composes feeds can run without them. Declare it in a service provider's boot method:

app/Providers/AppServiceProvider.php
php
use Storyfeed\Facades\Storyfeed;

public function boot(): void
{
    Storyfeed::withoutStorage();
}

With this declaration:

  • nothing is scheduled: neither storyfeed:curate nor storyfeed:prune --bursts;
  • saving, deleting or restoring a Feedable model writes nothing;
  • recording an activity throws Storyfeed\Exceptions\StorageDisabled, whether through Storyfeed::activity(), the storyfeed() helper, a Story class or Storyfeed::publish().

Composed feeds, feed sources, the feed file and the kits work as before. To install for this mode, pass --without-storage. The installer publishes the configuration and creates routes/feed.php, but publishes no migrations and adds nothing to .env:

bash
php artisan storyfeed:install --without-storage

storyfeed:doctor reports the mode as recording.without_storage and skips its tables and maintenance checks.

Without the declaration, a Feedable model on an app with no feed_snapshots table still saves, and writes no snapshot. Recording an activity needs the tables.

Released under the MIT License.