Computed Values in the Feed
Introduction
A menu item's activity says how many times the dish has been ordered. Orders keep arriving after the activity was recorded, but the menu item itself is never saved again. A count stored at publish time goes stale, and the feed shows an old number next to a current page.
A menu item's order count changes when an order is placed, even if the menu item itself is not saved. Compute the count when the feed is retrieved, so the feed shows the current value.
Computing a Count When the Feed Is Retrieved
Return a closure body from feedMedia(). Pass the model's orders relationship to $context->model(withCount: ['orders']):
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Storyfeed\Body\KeyValue;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedMedia;
class MenuItem extends Model implements Feedable
{
use InteractsWithFeed;
public function orders(): BelongsToMany
{
return $this->belongsToMany(Order::class);
}
public static function feedMedia(FeedContext $context): ?FeedMedia
{
return FeedMedia::make()
->body(
function () use ($context): KeyValue {
$menuItem = $context->model(withCount: ['orders']);
return KeyValue::make()
->title($menuItem?->name)
->items('Orders', $menuItem?->orders_count);
},
);
}
}<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Storyfeed\Body\KeyValue;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedContext;
use Storyfeed\FeedMedia;
class MenuItem extends Model implements Feedable
{
use InteractsWithFeed;
public function orders(): BelongsToMany
{
return $this->belongsToMany(Order::class);
}
public static function feedMedia(FeedContext $context): ?FeedMedia
{
return FeedMedia::make(
body: function () use ($context): KeyValue {
$menuItem = $context->model(withCount: ['orders']);
return KeyValue::make(
items: ['Orders' => $menuItem?->orders_count],
title: $menuItem?->name,
);
},
);
}
}Models load once per model class for the page, with the requested counts loaded together. See Deferring Body Construction for what happens when a body resolver throws.
The menu item uses a KeyValue body to display its current order count:
- Price
- $2.95
- Section
- Sundaes
- Available
- At the counter
- Orders
- 12
Choosing Stored or Computed Values
Compute a value on retrieval when a stale value would mislead someone using the page. For example, a page that lets people place orders should retrieve the current order count. The displayed value updates the next time the feed is retrieved.
Store a value in activity data when it describes history, such as a menu item's prices before and after a change. Use toFeed() for the model's own current facts, such as its name or description, which update with the model. See Choosing Stored or Current Values.