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

Containers & Context ​

Introduction ​

The context role records where an activity happened, such as the shop a dish belongs to. Use it to retrieve activities within that shop.

Containers can be nested beyond target and context: dish → menu → shop → mall. Each model declares parent() so involving($mall) finds activity beneath it. See Distant Relations.

Recording Context ​

Record the context when publishing. Storyfeed does not fill roles later, so a context filter can only find activities recorded with that context.

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

namespace App\Http\Controllers;

use App\Models\MenuItem;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Storyfeed\Facades\Storyfeed;

class MenuItemQuestionController extends Controller
{
    public function store(Request $request, MenuItem $menuItem): RedirectResponse
    {
        $note = $menuItem->notes()->create([
            'user_id' => $request->user()->id,
            'body' => $request->validate(['body' => 'required|string'])['body'],
        ]);

        Storyfeed::activity()
            ->by($request->user())
            ->action('ask', $note)
            ->on($menuItem)              // target: what the question is about
            ->context($menuItem->shop)   // context: the shop the dish belongs to
            ->publish();

        return back();
    }
}
php
<?php

namespace App\Http\Controllers;

use App\Models\MenuItem;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Storyfeed\Facades\Storyfeed;

class MenuItemQuestionController extends Controller
{
    public function store(Request $request, MenuItem $menuItem): RedirectResponse
    {
        $note = $menuItem->notes()->create([
            'user_id' => $request->user()->id,
            'body' => $request->validate(['body' => 'required|string'])['body'],
        ]);

        Storyfeed::record(
            verb: 'ask',
            object: $note,
            actor: $request->user(),
            target: $menuItem,           // what the question is about
            context: $menuItem->shop,    // the shop the dish belongs to
        );

        return back();
    }
}
Erica Sinclair
Is that free for life, or only for today?

Choosing Between Target and Context ​

RoleHoldsIn the Sentence
targetwhat the action was directed atasked about the dish
contextwhere it happenedin the shop

Use context when the target belongs to a container, such as a dish in a shop. Storyfeed never copies the target into context. If the target is the container itself, the target role is enough, and involving($shop) already finds the activity:

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

namespace App\Http\Controllers;

use App\Http\Requests\PlaceOrderRequest;
use App\Models\Shop;
use Illuminate\Http\RedirectResponse;
use Storyfeed\Facades\Storyfeed;

class OrderController extends Controller
{
    public function store(
        PlaceOrderRequest $request,
        Shop $shop,
    ): RedirectResponse {
        $order = $shop->orders()->create($request->validated());

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

        return to_route('orders.show', $order);
    }
}
php
<?php

namespace App\Http\Controllers;

use App\Http\Requests\PlaceOrderRequest;
use App\Models\Shop;
use Illuminate\Http\RedirectResponse;
use Storyfeed\Facades\Storyfeed;

class OrderController extends Controller
{
    public function store(
        PlaceOrderRequest $request,
        Shop $shop,
    ): RedirectResponse {
        $order = $shop->orders()->create($request->validated());

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

        return to_route('orders.show', $order);
    }
}
Erica Sinclair

Record every role that describes what happened, even if the headline omits it. Storyfeed also uses roles for filtering and grouping.

For containers above the target or context, declare each model's parent.

Set the context when you need to filter by it or include it in a headline:

UsageWhy It Needs context
Storyfeed::feed()->context($shop)finds only activities recorded with that context
:context in a headlinedisplays the entity recorded in the context role

Retrieving Activities in a Container ​

The context method filters activities recorded within the shop:

A controller, or wherever the feed is retrieved
php
use Storyfeed\Facades\Storyfeed;

Storyfeed::feed()->context($shop)->get();
Erica Sinclair
Is that free for life, or only for today?

The involving method also includes activities about the shop itself, such as its creation, and activity beneath its declared parent hierarchy.

Using Non-Model Containers ​

To use a name as the context, pass a string such as ->context('Saturday service'). Storyfeed creates a party for the name. The :context token displays it, and Storyfeed::feed()->context('Saturday service') retrieves its activities.

Each distinct name creates a separate party. To store a value without assigning a role, use ->data(['service' => $name]). It is returned in the activity's data and cannot be used as a headline role token.

Use Activity Scopes to supply context across a callback or HTTP request.

Released under the MIT License.