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 Rendering for examples.

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

Reading a Page ​

MethodReturns
foreach ($page as $item)each item as a FeedItem
$page->collect()the items as a Collection of FeedItem
$page->items()the items as the payload's arrays
$page->nextCursor()the cursor of the next page, or null

Call FeedItem::of($array) to wrap an existing array, such as one returned by items().

FeedItem ​

Item Methods ​

MethodReturnsPayload Field
kind()activity, group, or null for a summary phrasekind
isActivity(), isGroup()boolkind
isDigest()bool: a group with axis summaryaxis
id()?stringid
verb()?string, null for a summary row that spans verbsverb
publishedAt()?CarbonImmutable, null for a summary phrasepublished_at
headline()Headlineheadline_template, headline
missingHeadline()?Headline: the verb's headline when the activity is redundantmissing_headline_template, missing_headline
glyph()?stringglyph
intent()?stringglyph_intent
data()Fluentdata
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()?Entity
entity($role)?Entity for a role by name
actors(), objects(), targets(), contexts(), origins(), results(), instruments()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.

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
period()?string: hour, day, week or month on a summary rowperiod
phrases()Collection of FeedItem, one per verb on a summary rowphrases
phrasesTruncated()boolphrases_truncated

Summary phrases support the same methods: verb(), count(), headline(), glyph(), and role methods.

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
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 url; 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
any other tokenitself

Items Without a Headline ​

ItemDisplays
a groupits count: 5 activities
a summary rowthe actor once, then the phrases joined: Ana got a balloon and went on 3 rides
a summary phraseits verb and count: ride (3)
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
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 url, with its attributes; the escaped label otherwise
toString(), (string)its label, or fallback text when absentlabel
label()?stringlabel
url()?stringurl
type()?string: the morph aliastype
id()?stringid
role()?string: the role containing the entity
attributes()arrayattributes
isModal()boolmodal
data()Fluentdata
media()?Fluent with icon, image, preview, url, filesmedia
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
formerType()?string: the deleted model's morph aliastombstone.formerType
deletedAt()?CarbonImmutabletombstone.deleted

Entity also supports array access: $entity['label'] or get('media.preview.src').

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]
phrase:verb (:count)

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