Distant Relations
Introduction
Like Eloquent's hasManyThrough, a feed can reach distant relations. A dish belongs to a menu, the menu to a shop, and the shop to a mall. Use involving($mall) to retrieve activity on any dish beneath it.
In the sample world, the chain is USS Butterscotch → Seasonal menu → Scoops Ahoy → Starcourt Mall. Each model names its closest container through parent() in toFeed():
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedEntity;
class Dish extends Model implements Feedable
{
use InteractsWithFeed;
public function menu(): BelongsTo
{
return $this->belongsTo(Menu::class);
}
public function toFeed(): FeedEntity
{
return FeedEntity::make($this->name)->parent($this->menu);
}
}<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Storyfeed\Concerns\InteractsWithFeed;
use Storyfeed\Contracts\Feedable;
use Storyfeed\FeedEntity;
class Dish extends Model implements Feedable
{
use InteractsWithFeed;
public function menu(): BelongsTo
{
return $this->belongsTo(Menu::class);
}
public function toFeed(): FeedEntity
{
return FeedEntity::make(label: $this->name, parent: $this->menu);
}
}Describe Menu and Shop the same way, each with its own parent relationship. Mall has no parent:
| Model | Parent in toFeed() |
|---|---|
Dish | ->parent($this->menu) |
Menu | ->parent($this->shop) |
Shop | ->parent($this->mall) |
Mall | no parent |
Record the activity on the dish. The mall's feed includes it without a mall role:
use App\Models\Dish;
use App\Models\Mall;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Storyfeed\Facades\Storyfeed;
Route::post('/dishes/{dish}/publish', function (Request $request, Dish $dish) {
Storyfeed::activity()
->by($request->user())
->action('add', $dish)
->publish();
});
Route::get('/malls/{mall}/feed', function (Mall $mall) {
return Storyfeed::feed()->involving($mall)->get();
});use App\Models\Dish;
use App\Models\Mall;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Storyfeed\Facades\Storyfeed;
Route::post('/dishes/{dish}/publish', function (Request $request, Dish $dish) {
Storyfeed::record(
verb: 'add',
object: $dish,
actor: $request->user(),
);
});
Route::get('/malls/{mall}/feed', function (Mall $mall) {
return Storyfeed::feed()->involving($mall)->get();
});- Price
- $2.95
- Section
- Sundaes
- Available
- At the counter
- Orders
- 12
Deeper Hierarchies
The same declaration supports task → list → folder → project → workspace → tenant. Each model names one parent. A tenant feed uses the same involving($tenant) query as the mall feed:
use App\Models\Tenant;
use Illuminate\Support\Facades\Route;
use Storyfeed\Facades\Storyfeed;
Route::get('/tenants/{tenant}/feed', function (Tenant $tenant) {
return Storyfeed::feed()->involving($tenant)->get();
});At publication, Storyfeed follows parents from the object, target and context. Shared ancestors are recorded once per activity. The default limit is ten parent links per starting role. Set storyfeed.ancestors.max_depth to change it. A cycle or an unresolvable parent ends that chain; the activity remains available.
Parents may be Feedable models, registered external models, or parties. parent(null) ends a chain. An explicit context() still records a place outside the object's parent chain.
Keeping Actor and Place Separate
When a user works on a task in another tenant, the task's parents lead to that tenant. The user's own parent may be their home tenant. Walking the user's parents would put the other tenant's activity in the home tenant's feed, so the actor's parents are never followed. involving($user) still finds the user's direct participation. The origin, result, instrument, location and generator roles also contribute only their direct identities.
Excluding Distant Relations
To retrieve only the activity that names the mall itself, exclude its distant relations:
use App\Models\Mall;
use Illuminate\Support\Facades\Route;
use Storyfeed\Facades\Storyfeed;
Route::get('/malls/{mall}/own-feed', function (Mall $mall) {
return Storyfeed::feed()->involvingDirectly($mall)->get();
});use App\Models\Mall;
use Illuminate\Support\Facades\Route;
use Storyfeed\Facades\Storyfeed;
Route::get('/malls/{mall}/own-feed', function (Mall $mall) {
return Storyfeed::feed()->involving($mall, deep: false)->get();
});A cursor keeps the depth it was created with. Resuming it with the other depth throws an exception.
Declaring a Parent on Existing History
Activities published before a model declared parent() have no recorded path. To fill those paths from current parents, pause readers, publishers, queue workers and schedulers, then run:
php artisan storyfeed:participants --ancestors --missing --writers-pausedThe command adds ancestor rows only for roles whose path was never recorded. Recorded paths, including paths recorded without a parent, remain unchanged. Running it again adds nothing to roles it has already filled. It accepts the same --resume, --restart and --chunk options as the full rebuild below.
A snapshot that does not record whether its role had a parent counts as never recorded, and the command fills it from current parents.
The doctor reports ancestors.missing for each activity role with a declared parent and no recorded path.
Moving a Container
An activity keeps the path recorded when it was published. Moving a folder changes the path of new activities; existing activities remain under the old ancestors. Snapshot refreshes alone do not move recorded history.
To make history follow current parents, pause readers, publishers, queue workers and schedulers for the full rebuild, then run:
php artisan storyfeed:participants --ancestors --writers-pausedThe command processes history in chunks and stores a committed cursor. Keep readers and writers paused after an interruption, then continue:
php artisan storyfeed:participants --ancestors --writers-paused --resumeUse --restart instead of --resume to discard progress and start again. --chunk=500 controls batch size, from 1 to 1000. Without --missing, the command replaces every recorded path, including paths of already indexed activities. Keep parent declarations and container relationships unchanged throughout a run.
Recording Self-Acting Containers
A project that closes itself is both actor and object. Recording it as the object lets its parents reach the tenant:
use App\Models\Project;
use Illuminate\Support\Facades\Route;
use Storyfeed\Facades\Storyfeed;
Route::post('/projects/{project}/close', function (Project $project) {
$project->update(['closed_at' => now()]);
Storyfeed::activity()->by($project)->action('close', $project)->publish();
return response()->noContent();
});use App\Models\Project;
use Illuminate\Support\Facades\Route;
use Storyfeed\Facades\Storyfeed;
Route::post('/projects/{project}/close', function (Project $project) {
$project->update(['closed_at' => now()]);
Storyfeed::record(
verb: 'close',
object: $project,
actor: $project,
);
return response()->noContent();
});Checking Parent Chains
php artisan storyfeed:doctor --only=ancestors| Finding | Remedy |
|---|---|
ancestors.unresolvable | Restore the missing parent or correct parent(), then rebuild history if it should follow the corrected path. |
ancestors.actor_only | Record a self-acting container as the object too. Actors never supply the place path. |
Ancestry is stored for filtering. It does not add breadcrumb fields to the activity payload. The role filters continue to match their explicitly recorded roles.