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:
<?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:
| Source | Example |
|---|---|
name attribute | USS Butterscotch |
title attribute | Spring Menu |
| the type's noun and key | Ticket #42 |
| class name and key | Order #1042 |
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:
<?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
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}",
);
}
}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():
<?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:
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:
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.