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

Feedable Models ​

Introduction ​

A feedable model provides a label for its activities. Storyfeed supplies a default label; links and images are optional.

Making Models Feedable ​

Using Default Labels ​

Implement the Feedable interface and use the InteractsWithFeed trait:

app/Models/Order.php
php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;

class Order extends Model implements Feedable
{
    use InteractsWithFeed;
}

Without any configuration, Storyfeed guesses a model's feed label by checking commonly used columns, then the type's registered noun, falling back on the class name and key. It uses the first source that has a value:

SourceExample
name attributeUSS Butterscotch
title attributeSpring Menu
the type's noun and keyTicket #42
class name and keyOrder #1042
Erica Sinclair
Erica Sinclair placed Order #1035 with Scoops Ahoy

To implement the contract's methods yourself, see Implementing the Feedable Contract. To retrieve a model's activities, see Filtering by Entity or Role.

Customizing Model Labels ​

To set a model's label, define its toFeed method:

app/Models/Order.php
php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedEntity;

class Order extends Model implements Feedable
{
    use InteractsWithFeed;

    public function toFeed(): FeedEntity
    {
        return FeedEntity::make()->label("Order #{$this->reference}"); 
    }
}
php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedEntity;

class Order extends Model implements Feedable
{
    use InteractsWithFeed;

    public function toFeed(): FeedEntity
    {
        return FeedEntity::make(
            label: "Order #{$this->reference}", 
        );
    }
}
Erica Sinclair
Erica Sinclair placed Order #1035 with Scoops Ahoy

The toFeed method returns a FeedEntity containing the model's label. It may also name the closest container with ->parent($this->shop) (or the named argument parent: $this->shop). This makes activities on the model retrievable through its ancestors. See Distant Relations.

Storyfeed stores the entity as the model's snapshot: one saved copy of its label and values, shared by every activity the model appears in. InteractsWithFeed refreshes the snapshot on model saves while recording is enabled. Changes to its snapshot values can therefore appear on older activities too. Activity data preserves the values recorded for that event. A composed feed reads toFeed() each time it is read instead.

Describing the Snapshot with describeFeed() ​

Define snapshot values by modifying the entity returned by $this->feedEntity():

app/Models/Order.php
php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;

class Order extends Model implements Feedable
{
    use InteractsWithFeed;

    public function describeFeed(): void
    {
        $this->feedEntity()
            ->data(['reference' => $this->reference]); 
    }
}

This example adds snapshot data while retaining the default label. You may also set a label or combine values from a parent model and its subclasses. Unset fields remain empty except for the label. If you implement toFeed, the trait does not call describeFeed.

Customizing Default Labels ​

To customize default labels across your application, register a callback in a service provider. Return null to use the default rules:

app/Providers/AppServiceProvider.phpboot()
php
use Illuminate\Database\Eloquent\Model;
use Storyfeed\Facades\Storyfeed;

Storyfeed::guessFeedLabelsUsing(
    fn (Model $model) => $model->getAttribute('reference'),
);

To customize one model's default label, override its guessFeedLabel method. Overriding guessFeedLabel() bypasses the application-wide guesser. To call the trait's implementation from your override, alias it: use InteractsWithFeed { guessFeedLabel as guessedFeedLabel; }.

Registering External Models ​

To include a model from another package without modifying its class, register it in a service provider:

app/Providers/AppServiceProvider.phpboot()
php
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Storyfeed\Facades\Storyfeed;
use Storyfeed\FeedEntity;

Storyfeed::feedable(Media::class)
    ->toFeedUsing(
        fn (Media $photo, FeedEntity $entity) => $entity
            ->label($photo->name)
            ->data(['mediaType' => $photo->mime_type]),
    );

The closure is optional. Storyfeed treats the registered class as Feedable: saves refresh snapshots, and deletion and restoration update its activities. Register the exact instantiated class; parent registrations do not apply to subclasses. Classes implementing Feedable cannot also be registered. See the registration methods for the optional link resolver.

Linking Models and Showing Media ​

Use feedMedia() to resolve a model's links, files, pictures, and actor avatar when the feed is retrieved. See Feed Media for each job and its feed outcome.

For stored resolver inputs, see Storing Snapshot Data.

Released under the MIT License.