FeedItem API
Introduction
Storyfeed\Support\FeedItem, Headline, and Entity provide named methods for accessing the payload without changing it. See Building Your Own Components for examples.
@foreach ($page as $item)
{{ $item->headline() }}
@endforeachReading a Page
| Method | Returns |
|---|---|
Storyfeed::feed()->get() | a Collection of FeedItem |
$page = Storyfeed::feed()->cursorPaginate() | Laravel's cursor paginator of FeedItem; simplePaginate() and members() return the same items |
foreach ($page as $item) | each item of a paginator as a FeedItem |
$page->nextCursor() | the next page's Cursor (->encode() for the string), or null |
$page->syncToken() | the page's sync token, or null |
$item->toArray() | the item's payload array |
Call FeedItem::of($array) to wrap an existing array, such as an item from a page's JSON.
Group Anchors
A group's id encodes a Storyfeed\Grouping\Anchor, as a cursor string encodes Laravel's Cursor. members() takes either:
| Method | Returns |
|---|---|
Anchor::fromEncoded($id) | the anchor a group's id encodes, or null for a string that is not a group id |
$anchor->encode() | the group's id |
$anchor->parameter('axis'), parameter('hash') | one part; another name throws UnexpectedValueException |
$anchor->parameters(['axis', 'hash']), toArray() | the parts, as a list or keyed |
A group's id is opaque: store it and pass it back unchanged.
FeedItem
Item Methods
| Method | Returns | Payload Field |
|---|---|---|
kind() | activity or group | kind |
isActivity(), isGroup() | bool | kind |
id() | ?string | id |
verb() | ?string | verb |
publishedAt() | ?CarbonImmutable | published_at |
startsAt(), endsAt() | ?CarbonImmutable: the time range the activity describes | starts_at, ends_at |
headline() | Headline | headline_template, headline |
bindings() | array of name => text: the headline's bound values; [] when none | bindings |
missingHeadline() | ?Headline: the verb's headline when the activity is redundant | missing_headline_template, missing_headline |
glyph() | ?string | glyph |
intent() | ?string | glyph_intent |
data() | Fluent, with each value read through the verb's casts | data |
tombstoned() | array of role names | tombstoned |
isRedundant() | bool | redundant |
get($key, $default = null) | any key, with dot notation: get('object.label') | any |
toArray() | the original payload array |
Role Methods
| Method | Returns |
|---|---|
actor(), object(), target(), context(), origin(), result(), instrument(), location(), generator() | ?Entity |
entity($role) | ?Entity for a role by name |
actors(), objects(), targets(), contexts(), origins(), results(), instruments(), locations(), generators() | Collection of Entity |
entities($role) | Collection of Entity for a role by name |
distinct($role) | int: how many distinct entities hold the role |
For an activity, actor() returns its actor, actors() contains that entity or is empty, and distinct('actors') returns 1 or 0. For a group, actor() is set only when all members share it, actors() returns the sample, and distinct('actors') returns the full total. Singular and plural role names are equivalent: distinct('actor') and distinct('actors') return the same value.
The role methods cover the nine roles, not featured. Read the featured role and a group's featured strip with get():
$item->get('featured'); // the featured role's name, or null
$item->get('sample.featured'); // a group's members' featured entities, as arrays
$item->get('distinct.featured'); // how many members feature an entityGroup Methods
| Method | Returns | Payload Field |
|---|---|---|
count() | int: members, 1 for an activity | count |
children() | Collection of FeedItem, newest first | children |
childrenTruncated() | bool: count() is more than children() holds | children_truncated |
axis() | ?string | axis |
Array Access
FeedItem supports array access and JSON encoding of its underlying payload:
$item['verb'];
$item['sample']['actors'];
json_encode($item);Setting or unsetting a key throws LogicException; the wrapper is immutable.
Headline
| Method | Returns |
|---|---|
toHtml(), or echoing the headline in Blade | the headline as HTML: entity labels linked, text escaped |
toHtml(fn (Entity $entity) => …) | the headline, with each entity rendered by the closure, including an empty role as Entity::absent($role) |
toString(), (string) | the headline as plain text |
template() | ?string: headline_template |
isFallback() | bool: the item has no headline and uses Storyfeed's fallback text |
segments() | Collection of the headline's parts |
Tokens
| Token | Displays |
|---|---|
:actor, :object, … | the entity, linked when it has a link; on a group holding several, the list |
:actors, :objects, … | the sample, joined, and the rest as a number: Ana, Ben, Cy and 2 more |
:count | count() |
:others | the actors not in the sample: 2 others |
| a bound token | its value, as plain text |
| any other token | itself |
Items Without a Headline
| Item | Displays |
|---|---|
| a group | its count: 5 activities |
| an activity | its actor, verb and object: Dana confirm Order #1042 |
Segments
segments() returns headline parts in order, each with a type and plain text:
type | Also Has |
|---|---|
text | token and binding (its name) when the text is a bound value, escaped like other text |
entity | role, and entity: an Entity, or null when the role is empty |
entities | role, entities: a Collection of Entity, and total |
@foreach ($item->headline()->segments() as $segment)
@if ($segment['type'] === 'entity' && $segment['entity'])
<x-avatar :entity="$segment['entity']" />
@endif
{{ $segment['text'] }}
@endforeachEntity
| Method | Returns | Payload Field |
|---|---|---|
toHtml(), or echoing the entity in Blade | a link when it has a link, with its attributes; the escaped label otherwise | |
toString(), (string) | its label, or fallback text when absent | label |
label() | ?string | label |
url() | ?string | link.href |
type() | ?string: the morph alias | type |
id() | ?string | id |
role() | ?string: the role containing the entity | |
attributes() | array | link.attributes |
isModal() | bool | link.modal |
data() | Fluent | data |
media() | ?Fluent with icon, image, preview, initials, color, files, slots | media |
files() | Collection | media.files |
bodies() | Collection of bodies | body |
content(), mediaType(), attributedTo() | ?string | content, mediaType, attributedTo |
isDegraded() | bool: no label yet, and not deleted | label, tombstone |
isTombstone() | bool: the model was deleted | tombstone |
isAbsent() | bool: the role is empty, as an anonymous actor's is; it reads as its fallback text | |
formerType() | ?string: the deleted model's morph alias | tombstone.formerType |
deletedAt() | ?CarbonImmutable | tombstone.deleted |
Entity also supports array access: $entity['label'] or get('media.preview.src').
Echoing an entity in Blade writes the whole link. Inside a link of your own, echo $entity->label() instead, so the links don't nest.
Entity::absent($role) creates an entity for an empty role. It has no type, label or link, isAbsent() is true, and isDegraded() is false.
Translation Lines
Fallback text comes from storyfeed::feed in the current locale. Publish the translations to lang/vendor/storyfeed to customise them:
php artisan vendor:publish --tag=storyfeed-translations| Key | English |
|---|---|
someone | Someone |
something | Something |
former | a former :type |
removed | a removed :type |
item | item: fallback type for an unnamed deleted model |
and | and |
more | :count more |
others | :count other / :count others |
activities | :count activity / :count activities |
unnamed | :actor :verb[ :object] |
:type uses the former morph alias with spaces: line_item becomes line item.
Helpers
FeedItem, Headline, and Entity support when(), unless(), tap(), dump(), and dd() through Laravel's Conditionable, Tappable, and Dumpable.
Adding Methods
Each class supports macros. Register methods in a service provider's boot method:
use Storyfeed\Support\FeedItem;
FeedItem::macro('isPlacement', function (): bool {
return $this->verb() === 'place';
});@if ($item->isPlacement())
…
@endifCalling an unregistered method throws BadMethodCallException.