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

Keeping the Latest Activity ​

Introduction ​

keepLatest() keeps the latest matching activity in the feed and removes earlier ones, including from log().

Replacing Earlier Activities ​

routes/feed.php
php
use App\Models\Order;
use Storyfeed\Facades\Story;

Story::for(Order::class)->verb('save')
    ->headline(':actor saved :object')
    ->keepLatest(); // Earlier saves of this order are removed from log() too.

Publish as usual. The declaration applies wherever you publish this verb:

app/Http/Controllers/OrderController.php
php
<?php

namespace App\Http\Controllers;

use App\Http\Requests\UpdateOrderRequest;
use App\Models\Order;
use Illuminate\Http\RedirectResponse;
use Storyfeed\Facades\Storyfeed;

class OrderController extends Controller
{
    public function update(UpdateOrderRequest $request, Order $order): RedirectResponse
    {
        $order->update($request->validated());

        Storyfeed::activity()
            ->by($request->user())
            ->action('save', $order)
            ->publish();

        return back();
    }
}
php
<?php

namespace App\Http\Controllers;

use App\Http\Requests\UpdateOrderRequest;
use App\Models\Order;
use Illuminate\Http\RedirectResponse;
use Storyfeed\Facades\Storyfeed;

class OrderController extends Controller
{
    public function update(UpdateOrderRequest $request, Order $order): RedirectResponse
    {
        $order->update($request->validated());

        Storyfeed::record(
            verb: 'save',
            object: $order,
            actor: $request->user(),
        );

        return back();
    }
}

After several saves of the same order, the feed contains only the latest:

Erica Sinclair

Activities for other objects and verbs remain.

Choosing Every Occurrence or the Latest ​

Suppose an order is placed, confirmed, amended, and placed again:

RequestEvery OccurrencekeepLatest()
first placementappend placedreplace placed
confirmationappend confirmedreplace confirmed
placed again after an amendmentappend another placedreplace the earlier placed
visible rows afterwardfirst placement, confirmation, second placementconfirmation, second placement

Without a policy, the order's log keeps every placement:

Erica Sinclair
Steve Harrington
Erica Sinclair

With keepLatest() on each verb, it keeps the latest activity for each verb:

Erica Sinclair
Steve Harrington

Replaced activities disappear from every feed. Keep every activity if any page needs the full timeline.

Choosing Matching Roles ​

Replace the original save declaration with this one:

routes/feed.php
php
use App\Models\Order;
use Storyfeed\Facades\Story;

Story::for(Order::class)->verb('save')
    ->headline(':actor saved :object')
    ->keepLatest(per: ['object', 'actor']);
Steve Harrington
Erica Sinclair

The feed keeps each actor's latest save of each order. Pass one role name or an array to per; the verb is always part of the match. An activity replaces nothing if a matching role is empty. For example, an anonymous save has no recorded actor, so it replaces nothing under per: ['object', 'actor'].

Keeping the Latest Per Record ​

When the object is new each time, such as an interaction row, nothing ever matches. Key on the record the activity concerns instead:

routes/feed.php
php
use App\Models\DocumentInteraction;
use Storyfeed\Facades\Story;

Story::for(DocumentInteraction::class)->verb('engage')
    ->headline(':actor opened :target')
    ->keepLatest(per: 'target'); // Use 'context' to key on the context instead.

The feed keeps one engage activity per document, whichever interaction came last. See Keeping a Pulse Per Record for the full recipe.

Limiting the Time Window ​

Add a time window to that declaration:

routes/feed.php
php
use App\Models\Order;
use Storyfeed\Facades\Story;

Story::for(Order::class)->verb('save')
    ->headline(':actor saved :object')
    ->keepLatest(per: ['object', 'actor'], within: '10 minutes');
Erica Sinclair
Erica Sinclair

Only matching activities within ten minutes of the new activity's published_at are considered. Older saves outside the window remain. within accepts a positive interval string or a DateInterval.

DeclarationMatching Activities
keepLatest()same object and verb, without a time limit
keepLatest(per: ['object', 'actor'])same object, actor and verb
keepLatest(per: 'target')same target and verb, whatever the object
keepLatest(within: '10 minutes')same object and verb, within ten minutes
no declarationevery activity remains

Deleting Superseded Activities ​

By default, superseded activities are soft-deleted. They leave the feed but remain in the activities table until pruning. To delete them permanently as they are superseded:

config/storyfeed.php
php
'keep_latest' => [
    'delete' => 'force',
],
ValueSuperseded Activities
'soft'soft-deleted; the default
'force'deleted

Storyfeed keeps the latest published_at, regardless of arrival order. A backdated activity older than a matching one is stored as soft-deleted, or discarded under 'force'.

Comparing With Unique Stories ​

ShouldBeUnique keeps the first pending publication. keepLatest() replaces matching stored activities after publication.

Released under the MIT License.