Keeping the Latest Activity
Introduction
keepLatest() keeps the latest matching activity in the feed and removes earlier ones, including from log().
Replacing Earlier Activities
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:
<?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
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:
Activities for other objects and verbs remain.
Choosing Every Occurrence or the Latest
Suppose an order is placed, confirmed, amended, and placed again:
| Request | Every Occurrence | keepLatest() |
|---|---|---|
| first placement | append placed | replace placed |
| confirmation | append confirmed | replace confirmed |
| placed again after an amendment | append another placed | replace the earlier placed |
| visible rows afterward | first placement, confirmation, second placement | confirmation, second placement |
Without a policy, the order's log keeps every placement:
With keepLatest() on each verb, it keeps the latest activity for each verb:
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:
use App\Models\Order;
use Storyfeed\Facades\Story;
Story::for(Order::class)->verb('save')
->headline(':actor saved :object')
->keepLatest(per: ['object', 'actor']);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:
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:
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');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.
| Declaration | Matching 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 declaration | every 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:
'keep_latest' => [
'delete' => 'force',
],| Value | Superseded 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.