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

Resolving Bodies When Retrieved ​

A body built in feedMedia() shows the model’s current values instead of the stored snapshot.

Using Current Values ​

Return a body from feedMedia() to use the model's current values:

app/Models/MenuItem.php
php
<?php

namespace App\Models;

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

class MenuItem extends Model implements Feedable
{
    use InteractsWithFeed;

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

    public static function feedMedia(FeedContext $context): ?FeedMedia
    {
        return FeedMedia::make()
            ->body(
                KeyValue::make()
                    ->items('Portions left', $context->model()?->portions_left),
            );
    }
}
php
<?php

namespace App\Models;

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

class MenuItem extends Model implements Feedable
{
    use InteractsWithFeed;

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

    public static function feedMedia(FeedContext $context): ?FeedMedia
    {
        return FeedMedia::make(
            body: KeyValue::make(
                items: ['Portions left' => $context->model()?->portions_left],
            ),
        );
    }
}

Stored and resolved bodies share the same payload shape. When toFeed() and feedMedia() both return bodies, the item includes both, with stored bodies first. Your renderer controls the layout.

Choosing Stored or Current Values ​

Choose when a value is decided:

MethodWhen It RunsValue
->data(…) on the activitywhen the activity is publishedfrozen at publication
->body(…) on FeedEntity in toFeed()whenever the model is savedstored and updated with the model
->body(…) on FeedEntity in toFeed(), in a composed feedwhenever the feed is retrievedbuilt from current values and never stored
->body(…) on FeedMedia in feedMedia()whenever the feed is retrievedbuilt from current values and never stored

See Computed Values in the Feed for a count computed on retrieval.

Deferring Body Construction ​

The resolver runs whenever the feed is retrieved. Pass a closure to defer building the body until the payload needs it:

app/Models/MenuItem.php
php
<?php

namespace App\Models;

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

class MenuItem extends Model implements Feedable
{
    use InteractsWithFeed;

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

    public static function feedMedia(FeedContext $context): ?FeedMedia
    {
        return FeedMedia::make()->body(
            fn (): KeyValue => KeyValue::make()
                ->items('Portions left', $context->model()?->portions_left),
        );
    }
}
php
<?php

namespace App\Models;

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

class MenuItem extends Model implements Feedable
{
    use InteractsWithFeed;

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

    public static function feedMedia(FeedContext $context): ?FeedMedia
    {
        return FeedMedia::make(
            body: fn (): KeyValue => KeyValue::make(
                items: ['Portions left' => $context->model()?->portions_left],
            ),
        );
    }
}

Loading models takes one query per model class on the page. Use a closure when the body needs current model data; bodies built from the snapshot can be passed directly.

Storyfeed reports a thrown exception once per model class:

What ThrowsWhat the Entity Loses
a deferred body closurethat body only; the entity keeps its label, link and other bodies
feedMedia() itselfeverything feedMedia() returns: its bodies, links and pictures

Accessing Resolver Data ​

The resolver runs for every entity on the page. Use $context->data() for the snapshot or $context->model() for the current model. The latter loads all models of that class on the page together. Pass relations to $context->model(with: […]) to load them together too. A query such as $model->orders()->count() runs once per entity. Pass withCount: instead to count for every model of the class in one query:

app/Models/MenuItem.phpfeedMedia()
php
$orders = $context->model(withCount: ['orders'])?->orders_count;

See Activity Content for stored bodies and Feed Media for links, files, pictures and avatars.

Released under the MIT License.