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

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.

blade
@foreach ($page as $item)
    {{ $item->headline() }}
@endforeach

Reading a Page ​

MethodReturns
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:

MethodReturns
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 ​

MethodReturnsPayload Field
kind()activity or groupkind
isActivity(), isGroup()boolkind
id()?stringid
verb()?stringverb
publishedAt()?CarbonImmutablepublished_at
startsAt(), endsAt()?CarbonImmutable: the time range the activity describesstarts_at, ends_at
headline()Headlineheadline_template, headline
bindings()array of name => text: the headline's bound values; [] when nonebindings
missingHeadline()?Headline: the verb's headline when the activity is redundantmissing_headline_template, missing_headline
glyph()?stringglyph
intent()?stringglyph_intent
data()Fluent, with each value read through the verb's castsdata
tombstoned()array of role namestombstoned
isRedundant()boolredundant
get($key, $default = null)any key, with dot notation: get('object.label')any
toArray()the original payload array

Role Methods ​

MethodReturns
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():

php
$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 entity

Group Methods ​

MethodReturnsPayload Field
count()int: members, 1 for an activitycount
children()Collection of FeedItem, newest firstchildren
childrenTruncated()bool: count() is more than children() holdschildren_truncated
axis()?stringaxis

Array Access ​

FeedItem supports array access and JSON encoding of its underlying payload:

php
$item['verb'];
$item['sample']['actors'];
json_encode($item);

Setting or unsetting a key throws LogicException; the wrapper is immutable.

Headline ​

MethodReturns
toHtml(), or echoing the headline in Bladethe 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 ​

TokenDisplays
: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
:countcount()
:othersthe actors not in the sample: 2 others
a bound tokenits value, as plain text
any other tokenitself

Items Without a Headline ​

ItemDisplays
a groupits count: 5 activities
an activityits actor, verb and object: Dana confirm Order #1042

Segments ​

segments() returns headline parts in order, each with a type and plain text:

typeAlso Has
texttoken and binding (its name) when the text is a bound value, escaped like other text
entityrole, and entity: an Entity, or null when the role is empty
entitiesrole, entities: a Collection of Entity, and total
blade
@foreach ($item->headline()->segments() as $segment)
    @if ($segment['type'] === 'entity' && $segment['entity'])
        <x-avatar :entity="$segment['entity']" />
    @endif
    {{ $segment['text'] }}
@endforeach

Entity ​

MethodReturnsPayload Field
toHtml(), or echoing the entity in Bladea link when it has a link, with its attributes; the escaped label otherwise
toString(), (string)its label, or fallback text when absentlabel
label()?stringlabel
url()?stringlink.href
type()?string: the morph aliastype
id()?stringid
role()?string: the role containing the entity
attributes()arraylink.attributes
isModal()boollink.modal
data()Fluentdata
media()?Fluent with icon, image, preview, initials, color, files, slotsmedia
files()Collectionmedia.files
bodies()Collection of bodiesbody
content(), mediaType(), attributedTo()?stringcontent, mediaType, attributedTo
isDegraded()bool: no label yet, and not deletedlabel, tombstone
isTombstone()bool: the model was deletedtombstone
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 aliastombstone.formerType
deletedAt()?CarbonImmutabletombstone.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:

bash
php artisan vendor:publish --tag=storyfeed-translations
KeyEnglish
someoneSomeone
somethingSomething
formera former :type
removeda removed :type
itemitem: fallback type for an unnamed deleted model
andand
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:

app/Providers/AppServiceProvider.phpboot()
php
use Storyfeed\Support\FeedItem;

FeedItem::macro('isPlacement', function (): bool {
    return $this->verb() === 'place';
});
blade
@if ($item->isPlacement())
    …
@endif

Calling an unregistered method throws BadMethodCallException.

Released under the MIT License.