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

Building Your Own Components ​

Introduction ​

Build your own Blade components to render activity headlines, bodies, groups, and pagination. For ready-made components, use Storyfeed UI.

Building Your Own Components ​

These components carry no styling. They are your application's own anonymous components in resources/views/components/feed, so Blade names them <x-feed>, <x-feed.item>, and so on, separately from Storyfeed UI's <x-storyfeed::feed>.

The Feed Components ​

Create these anonymous Blade components in resources/views/components/feed:

ComponentFileRenders
<x-feed>feed.blade.phpthe feed and pagination link
<x-feed.item>item.blade.phpan activity or group
<x-feed.activity>activity.blade.phpan activity row
<x-feed.group>group.blade.phpa group row and its members
<x-feed.glyph>glyph.blade.phpthe icon
<x-feed.time>time.blade.phpthe publication time
<x-feed.body>body.blade.php, body/key-value.blade.php, …a body using its type

Blade renders feed/feed.blade.php as <x-feed> because the file name matches its directory. See Laravel's anonymous index components.

Accessing Feed Items ​

Iterating over a feed page returns each item as a Storyfeed\Support\FeedItem. Use its methods to access the payload:

resources/views/feed.blade.php
blade
@foreach ($page as $item)
    {{ $item->headline() }}
    {{ $item->actor()?->label() }}
    {{ $item->publishedAt()?->diffForHumans() }}
@endforeach
Erica Sinclair placed Order #1035 with Scoops Ahoy
Erica Sinclair

Echo $item->headline() to render the headline with linked entity labels. Items also support array access, such as $item['verb'], and toArray() returns the item's payload array. See FeedItem API for all methods.

Parts of a Row ​

Part of a RowMethodsPayload Fields
iconglyph(), intent(), actor()glyph, glyph_intent, actor
headlineheadline()headline_template or headline, the role keys
timepublishedAt()published_at
mediaobject()->media()object.media
bodyobject()->bodies()an entity's body list
group imagesactors(), distinct('actors')a group's sample, distinct
group memberschildren(), count()children, count

Omit elements whose corresponding fields are empty.

Rendering Activities ​

Headlines ​

To render a headline in Blade, echo the value returned by the headline method:

resources/views/components/feed/activity.blade.php
blade
{{ $activity->headline() }}

The headline replaces role tokens in headline_template with entity labels. Entities with a link render as links with its attributes, such as target. If the item contains a completed headline, that text is displayed. Other text is escaped.

The headline is a sentence with links in it, so never wrap it in another link. See Links in a Headline.

Use the toString method to return plain text for a page title or notification:

A view that shows one activity
blade
<title>{{ $activity->headline()->toString() }}</title>

Each role method returns a Storyfeed\Support\Entity, or null for an empty role. Echo the entity to display its label, linked when it has a URL:

resources/views/components/feed/activity.blade.php
blade
{{ $activity->object() }}

Use the label, url, and type methods to access individual values.

To customize entity markup, pass a closure to the toHtml method. It receives each Entity and returns HTML. Escape values included in that HTML. An empty role, such as an anonymous actor, reaches the closure too, as an entity whose isAbsent() is true and that reads as Someone:

resources/views/components/feed/activity.blade.php
blade
@use('Storyfeed\Support\Entity')

{!! $activity->headline()->toHtml(
    fn (Entity $entity) => '<strong>'.$entity->toHtml().'</strong>'
) !!}

Timestamps ​

The publishedAt method returns published_at as a CarbonImmutable instance, or null for a dateless entry:

resources/views/components/feed/time.blade.php
blade
@props(['at'])

@if ($at)
    <time datetime="{{ $at->toAtomString() }}" {{ $attributes }}>
        {{ $at->diffForHumans() }}
    </time>
@endif

Icons and Intents ​

The glyph method returns the registered icon identifier, such as shopping-bag. Provide a view for each icon and a fallback for unknown values:

resources/views/components/feed/glyph.blade.php
blade
@props(['glyph', 'intent' => null])

<span {{ $attributes->merge(['data-intent' => $intent]) }}>
    @includeFirst(["icons.{$glyph}", 'icons.activity'])
</span>

The intent method returns the application-defined value used to style the icon:

success
pending

Define intent values with the verb's intent method. Storyfeed provides no default values or colours and does not validate these strings. Map values such as success, pending, and danger to your CSS, for example with a [data-intent="success"] selector.

If no intent is defined, the method returns null and the component omits data-intent. Undefined colours leave the icon's default styling unchanged.

Activity Rows ​

Combine the icon, headline, and timestamp in the activity component:

resources/views/components/feed/activity.blade.php
blade
@props(['activity'])

<article {{ $attributes->class('flex items-center gap-3') }}>
    <x-feed.glyph
        :glyph="$activity->glyph()"
        :intent="$activity->intent()"
    />
    <div class="flex-1">{{ $activity->headline() }}</div>
    <x-feed.time :at="$activity->publishedAt()" />
</article>

Rendering Groups ​

Group Rows ​

Use the isGroup method to identify a group. The children method returns its members as feed items for the activity component to render. See Aggregation for grouping rules and headline tokens.

resources/views/components/feed/group.blade.php
blade
@props(['group'])

<article {{ $attributes }}>
    <x-feed.glyph
        :glyph="$group->glyph()"
        :intent="$group->intent()"
    />
    <div>{{ $group->headline() }}</div>
    <x-feed.time :at="$group->publishedAt()" />

    <details @if ($group->headline()->isFallback()) open @endif>
        <summary>{{ $group->count() }} activities</summary>

        @foreach ($group->children() as $child)
            <x-feed.activity :activity="$child" />
        @endforeach
    </details>
</article>
Robin BuckleyDustin Henderson
Robin BuckleyDustin HendersonErica Sinclair

The count method returns the total member count. If children contains fewer members, childrenTruncated returns true.

Plural Roles ​

Plural tokens such as :actors display the sampled entities and a count of the remainder: "Ana, Ben, Cy and 2 more". A singular token such as :actor displays one entity when all members share it, or the list otherwise. The :count token displays the group's activity count.

Robin BuckleyDustin Henderson
Robin BuckleyDustin HendersonErica Sinclair

To render the sample as images, get the role's entities and total count:

resources/views/components/feed/group.blade.php<article>
blade
@foreach ($group->actors() as $actor)
    <img
        src="{{ $actor->media()?->get('icon.src') }}"
        alt="{{ $actor->label() }}"
    >
@endforeach

@if ($group->distinct('actors') > $group->actors()->count())
    +{{ $group->distinct('actors') - $group->actors()->count() }}
@endif

Groups Without Headlines ​

If neither a group headline nor the single-activity headline applies, both payload headline fields are null. The PHP reader's $group->headline() returns a Headline value: isFallback() is true, and toString() returns the translated count, such as “3 activities”. An explicitly returned empty string does not use this null-field fallback.

To style the fallback headline separately, add a class to it:

resources/views/components/feed/group.blade.php<article>
blade
<div @class(['muted' => $group->headline()->isFallback()])>
    {{ $group->headline() }}
</div>
Robin BuckleyDustin Henderson
3 activities
Mon 1 Jul, 12:02 PM
Mon 1 Jul, 12:01 PM
Mon 1 Jul, 12:00 PM

See Fallback Headlines.

Rendering Content ​

The data method returns values stored with the activity. Choose which values to display.

Bodies ​

The bodies method returns an entity's structured content. Render the object's bodies in the activity row:

resources/views/components/feed/activity.blade.php<article>
blade
@foreach ($activity->object()?->bodies() ?? [] as $body)
    <x-feed.body :body="$body" />
@endforeach

Each body's $body field identifies its type, such as Storyfeed/Body/KeyValue. The component maps each $body identifier to a class and a view, upgrades older versions, and skips types it does not know:

resources/views/components/feed/body.blade.php
blade
@props(['body'])

@php
    use Storyfeed\Body\Excerpt;
    use Storyfeed\Body\KeyValue;

    $renderers = [
        KeyValue::bodyType() => [KeyValue::class, 'feed.body.key-value'],
        Excerpt::bodyType() => [Excerpt::class, 'feed.body.excerpt'],
    ];
    $renderer = $renderers[$body['$body'] ?? ''] ?? null;
    $version = $body['$v'] ?? 1;
    $component = null;

    if ($renderer !== null && is_int($version) && $version >= 1) {
        [$class, $view] = $renderer;

        if ($version <= $class::version()) {
            $body = $class::upgrade($body, $version);
            $component = $view;
        }
    }
@endphp

@if ($component !== null)
    <x-dynamic-component :component="$component" :body="$body" />
@endif

Add a component for each body type you render. This KeyValue component displays the placeholder when a value is null, or an empty string when both are null:

resources/views/components/feed/body/key-value.blade.php
blade
@props(['body'])

<dl {{ $attributes }}>
    @foreach ($body['items'] as $item)
        <dt>{{ $item['key'] }}</dt>
        <dd>{{ $item['value'] ?? $item['placeholder'] ?? '' }}</dd>
    @endforeach
</dl>
Pickup
12:10 pm
Items
1
Reference
1035
Table
not seated

An Excerpt body holds quoted words. This component shows the attribution only when from has a value:

resources/views/components/feed/body/excerpt.blade.php
blade
@props(['body'])

<figure {{ $attributes }}>
    <blockquote>
        {{ $body['text'] }}@if ($body['truncated'] ?? false)…@endif
    </blockquote>

    @if ($body['from'] ?? null)
        <figcaption>{{ $body['from'] }}</figcaption>
    @endif
</figure>
Planck’s constant is 6.62607004.
Suzie, over Cerebro

See Activity Content for body types and fields, or Custom Body Types to define your own.

Handling Missing Values ​

An entity's label and link may be null. An anonymous activity has no recorded actor and uses a placeholder in its headline:

Someone placed Something with Scoops Ahoy
ConditionDisplayCheck
an anonymous actorSomeoneactor() is null; in a toHtml closure, isAbsent()
a missing labelSomeone for the actor, Something for another roleisDegraded()
a deleted modela removed order, a former customer, based on its former typeisTombstone(), formerType()
a group without a headline5 activitiesheadline()->isFallback()

An entity without a URL is displayed as plain text. For anonymous activities, you may use a headline without an actor token.

Placeholders use Storyfeed's translation strings for the current locale. Publish the language file to customize them:

bash
php artisan vendor:publish --tag=storyfeed-translations

The command publishes lang/vendor/storyfeed/en/feed.php.

Assembling the Feed ​

The item component selects the component matching the item's kind:

resources/views/components/feed/item.blade.php
blade
@props(['item'])

@if ($item->isActivity())
    <x-feed.activity :activity="$item" />
@elseif ($item->isGroup())
    <x-feed.group :group="$item" />
@endif
Robin BuckleyDustin Henderson
Robin BuckleyDustin HendersonErica Sinclair
Erica Sinclair

The feed component renders each item, followed by the pagination link:

resources/views/components/feed/feed.blade.php
blade
@props(['page'])

<div role="feed" {{ $attributes }}>
    @foreach ($page as $item)
        <x-feed.item :item="$item" />
    @endforeach
</div>

{{ $page->links() }}

Attributes such as <x-feed :page="$page" class="…" /> are applied to the feed's root element.

The links method renders Laravel's simple pagination view. You may customize it through Laravel's pagination views. Feeds support forward pagination only; the previous-page link is disabled and no links appear on the last page.

Displaying the Feed ​

Pass a paginator to the view. The cursorPaginate method retrieves the current request's cursor for subsequent pages:

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

Route::get('/', function () {
    return view('feed', [
        'page' => Storyfeed::feed()->cursorPaginate(15)->withQueryString(),
    ]);
});

Render the feed with the x-feed component:

resources/views/feed.blade.php
blade
<x-feed :page="$page" />
Robin BuckleyDustin Henderson
Robin BuckleyDustin HendersonErica Sinclair
Steve Harrington
Erica Sinclair

For Vue components and Inertia usage, see the Vue kit.

Released under the MIT License.