This package enables you to have activity streams in your laravel applications.
Install with composer
composer require musonza/laravel-activity-streams
Once the composer installation is finished, you can add alias for the facade. Open config/app.php
, and make the following update:
-
Add a new item to the
aliases
array:'ActivityStreams' => Musonza\ActivityStreams\ActivityStreamsFacade::class,
-
Publish the configuration file into your app's
config
directory, by running the following command:php artisan vendor:publish --tag="activity.streams.config"
-
Publish the migrations into your app's
migrations
directory, by running the following command:php artisan vendor:publish --tag="activity.streams.migrations"
-
Run the migrations:
php artisan migrate
Whenever you use the ActivityStreams
facade in your code, remember to add the following line to your namespace imports:
use ActivityStreams;
Use the HasFeed
trait to allow a model to have a feed.
<?php
use Illuminate\Database\Eloquent\Model;
use Musonza\ActivityStreams\Traits\HasFeed;
class User extends Model
{
use HasFeed;
}
After adding the HasFeed
trait you can create a feed for the Model as follows
$feed = $user->createFeed();
Sometimes you may want to create a Feed that's does not belong to a Model. For example, you want to add activities to a Trending Feed for your application:
Create a class to represent the Trending feed under a namespace of choice
<?php
namespace App;
class Trending
{
// Optional implementation
}
Then run an artisan command that ships with the package
php artisan streams:make:feed 'App\Trending' 'some-unique-id'
some-unique-id
is unique with respect to $table->unique(['some-unique-id', 'App\Trending']);
An example of an activity will be something like John liked a photo in 2018Album
Actor | John |
Verb | like |
Object | photo |
Target | 2018Album |
use ActivityStreams;
use Musonza\ActivityStreams\ValueObjects\Verbs;
$activity = ActivityStreams::setActor($actor)
->setVerb(Verbs::VERB_LIKE)
->setObject($object)
->setTarget($target)
->createActivity();
You can pass in an Eloquent Model as an actor or any Object that implements Musonza\ActivityStreams\Contracts\ActivityActor
interface
You can pass in an Eloquent Model as a target or any Object that implements Musonza\ActivityStreams\Contracts\ActivityTarget
interface
You can pass in an Eloquent Model as an object or any Object that implements Musonza\ActivityStreams\Contracts\ActivityObject
interface
$verbs = ActivityStreams::verbs();
ActivityStreams::addActivityToFeed($feed, $activity);
Adds a Collection
of activities to a Feed
ActivityStreams::addActivityToFeed($feed, $activities);
Adds an Activity
to a Collection
feeds
ActivityStreams::addActivityToMultipleFeeds($feeds, $activity);
You can leverage and listen for the following events to perform actions in
your application. For instance you can listen for an ActivityCreated
event and depending on
your business logic add the created event to a Feed
or multiple feeds.
Musonza\ActivityStreams\Models\Activity\ActivityCreated
Musonza\ActivityStreams\Models\Activity\ActivityDeleted
Musonza\ActivityStreams\Models\Activity\FeedCreated
Musonza\ActivityStreams\Models\Activity\FeedDeleted
See more on Activity Streams specifications here