PHP code example of edram / laravel-interactions

1. Go to this page and download the library: Download edram/laravel-interactions library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

edram / laravel-interactions example snippets


// config/laravel-interactions.php
'default_actor' => App\Models\User::class,

use Edram\LaravelInteractions\Concerns\Interactable;
use Edram\LaravelInteractions\Concerns\Interacts;

class User extends Authenticatable
{
    use Interactable;
    use Interacts;
}

class Post extends Model
{
    use Interactable;
}

class Comment extends Model
{
    use Interactable;
}

use Edram\LaravelInteractions\Concerns\Actor\Follows;
use Edram\LaravelInteractions\Concerns\Actor\HasInteractions;
use Edram\LaravelInteractions\Concerns\Actor\Likes;
use Edram\LaravelInteractions\Concerns\Target\HasFollowers;
use Edram\LaravelInteractions\Concerns\Target\HasLikers;
use Edram\LaravelInteractions\Concerns\Target\ReceivesInteractions;

class User extends Authenticatable
{
    use HasInteractions;
    use Follows;
    use Likes;
    use ReceivesInteractions;
    use HasFollowers;
}

class Post extends Model
{
    use ReceivesInteractions;
    use HasLikers;
}

class Team extends Model
{
    use Interactable;
    use Interacts;
}

$user->like($post);
$team->like($post);

$post->likers()->get();            // User from default_actor
$post->likers(Team::class)->get(); // Team

$user->follow($team);
$team->followers()->get();         // User from default_actor
$team->followers(Team::class)->get();

$likes = $post->receivedInteractions('like')
    ->with('actor')
    ->get();

$actors = $likes->pluck('actor')->filter();

$user->like($post);
$user->unlike($post);
$user->toggleLike($post);
$user->hasLiked($post);
$post->isLikedBy($user);
$post->likers;
$user->likedItems(Post::class)->paginate();

$user->favorite($post);
$user->unfavorite($post);
$post->favoriters;

$user->subscribe($post);
$user->unsubscribe($post);
$post->subscribers;

$user->follow($anotherUser);
$user->unfollow($anotherUser);
$user->following;
$anotherUser->followers;

$user->upvote($post);
$user->downvote($post, 3);
$user->vote($post, 5);
$user->cancelVote($post);
$post->voters;
$post->upvoters;
$post->downvoters;
$post->totalVotes();

$post->likersFor(Team::class)->paginate();
$post->interactors('bookmark', Team::class)->get();

$interaction = $user->interact(
    $post,
    'bookmark',
    metadata: ['folder' => 'reading'],
);

$user->hasInteracted($post, 'bookmark');
$user->toggleInteraction($post, 'bookmark');
$user->uninteract($post, 'bookmark');

$user->interactions('bookmark')->get();
$post->receivedInteractions('bookmark')->with('actor')->get();
$user->interactedItems('bookmark', Post::class)->paginate();
$post->interactors('bookmark', User::class)->paginate();

$posts = Post::paginate();

$user->attachInteractionStatus($posts, ['like', 'favorite', 'vote']);

$posts[0]->interaction_status;
// ['like' => true, 'favorite' => null, 'vote' => -1]
bash
composer vendor:publish --tag="laravel-interactions-config"
php artisan vendor:publish --tag="laravel-interactions-migrations"
php artisan migrate