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.
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.
Storyfeed::activity()
->by($request->user())
->action('ask', $note)
->on($dish) // target: what the question is about
->context($shop) // context: the shop the dish belongs to
->publish();Storyfeed::record(
verb: 'ask',
object: $note,
actor: $request->user(),
target: $dish, // what the question is about
context: $shop, // the shop the dish belongs to
);Choosing Between Target and Context
| Role | Holds | In the Sentence |
|---|---|---|
target | what the action was directed at | asked about the dish |
context | where it happened | in the shop |
Use context when the target belongs to a container, such as a dish in a shop. If the target is the container itself, the target role is enough:
Storyfeed::activity()
->by($request->user())
->action('place', $order)
->to($shop)
->publish();Storyfeed::record(
verb: 'place',
object: $order,
actor: $request->user(),
target: $shop,
);Record every role that describes what happened, even if the headline omits it. Storyfeed also uses roles for filtering and grouping.
Set the context when you need to filter by it or include it in a headline:
| Usage | Why It Needs context |
|---|---|
Storyfeed::feed()->context($shop) | finds only activities recorded with that context |
:context in a headline | displays the entity recorded in the context role |
Reading Activities in a Container
The context method filters activities recorded within the shop. The involving method also includes activities about the shop itself, such as its creation.
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.