Usage

All methods available on this package to be used

This package offers any of the entities previously configured (check Getting started) the ability to save different type of interactions between them, by default but not only limited to you have the following interaction types:

  • Follow

  • Like

  • Subscribe

  • Participation

  • Bookmark

If you ever feel this is not enough you are completely free to send a PR to our repository or even better extend the enum and change it from the config file.

Save interactions

Knowing this you can simply use this package from the enum in the following way:

use OpenSoutheners\LaravelUserInteractions\Support\Facades\Interaction;


// User ID 1 will follow User ID 2
Interaction::from(User::find(1))
    ->to(User::find(2))
    ->does(UserInteractionType::Follow); // returns UserInteraction persisted model

Not a fan of Facades? You can use the functional way as well:

use OpenSoutheners\LaravelUserInteractions\UserInteractionType;

app('user.interaction')->from(User::find(1))
    ->to(User::find(2))
    ->does(UserInteractionType::Follow);

But you can do this in a even shorter way and without any import like this:

app('user.interaction')->followed(User::find(1), User::find(2));

// Or using a much descriptive way with named arguments
app('user.interaction')->followed(causer: User::find(1), subject: User::find(2));

// Or even a combination of both ways
app('user.interaction')->from(User::find(1))->followed(User::find(2));

Possibilities are endless!

use OpenSoutheners\LaravelUserInteractions\Support\Facades\Interaction;

Interaction::followed(User::find(1), User::find(2));
Interaction::liked(User::find(1), Post::find(2));
Interaction::subscribed(User::find(1), Channel::find(1));
Interaction::participated(User::find(1), Comment::find(3));
Interaction::bookmarked(User::find(1), Post::find(4));

Toggle removal when interaction exists

If your interaction logic needs to toggle whenever the same type and entities are at the same direction you can use the following:

// If this is the first interaction it will be saved otherwise it does nothing
Interaction::followed(User::find(1), User::find(2));

// This does the same but if one exists it will remove it
Interaction::toggle()->followed(User::find(1), User::find(2));

Checking existence of interaction

Now the previous code was for saving and you can check all the previously saved interactions using the following:

use OpenSoutheners\LaravelUserInteractions\Support\Facades\Interaction;
use OpenSoutheners\LaravelUserInteractions\UserInteractionType;

// Check if User ID 1 did follow User ID 2
Interaction::from(User::find(1))
    ->to(User::find(2))
    ->did(UserInteractionType::Follow); // returns bool

Or even shorter version:

use OpenSoutheners\LaravelUserInteractions\Support\Facades\Interaction;

Interaction::from(User::find(1))->to(User::find(2))->hasFollowed();
Interaction::from(User::find(1))->hasFollowed(User::find(2));
Interaction::hasFollowed(User::find(1), User::find(2));
Interaction::hasFollowed(causer: User::find(1), subject: User::find(2));

And of course this is compatible with any other interaction (even those one you added into your own enum!).

But how about checking if a user has been followed by the other user, you can revert the causer/subject but we prepared a more semantic method for you:

use OpenSoutheners\LaravelUserInteractions\Support\Facades\Interaction;

// To check if User ID 1 has been followed by User ID 2...

// you will normally do this
Interaction::hasFollowed(causer: User::find(2), subject: User::find(1));

// but you can also do this
Interaction::hasBeenFollowed(causer: User::find(1), subject: User::find(2));

Query interactions

You can save and check for interactions but what about querying? We got you covered:

use OpenSoutheners\LaravelUserInteractions\Support\Facades\Interaction;
use OpenSoutheners\LaravelUserInteractions\UserInteractionType;

// Query all followers of a user
Interaction::to(User::find(1))->doing(UserInteractionType::Follow)->get();

// Remove all followers of a user
Interaction::to(User::find(1))->doing(UserInteractionType::Follow)->delete();

// Query all follows of a user
Interaction::from(User::find(1))->doing(UserInteractionType::Follow)->get();

// Remove all follows of a user
Interaction::from(User::find(1))->doing(UserInteractionType::Follow)->delete();

Now is up to you to put all this in practice!

Last updated