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:
useOpenSoutheners\LaravelUserInteractions\Support\Facades\Interaction;// User ID 1 will follow User ID 2Interaction::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:
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 argumentsapp('user.interaction')->followed(causer:User::find(1), subject:User::find(2));// Or even a combination of both waysapp('user.interaction')->from(User::find(1))->followed(User::find(2));
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 nothingInteraction::followed(User::find(1),User::find(2));// This does the same but if one exists it will remove itInteraction::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:
useOpenSoutheners\LaravelUserInteractions\Support\Facades\Interaction;useOpenSoutheners\LaravelUserInteractions\UserInteractionType;// Check if User ID 1 did follow User ID 2Interaction::from(User::find(1))->to(User::find(2))->did(UserInteractionType::Follow); // returns bool
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:
useOpenSoutheners\LaravelUserInteractions\Support\Facades\Interaction;// To check if User ID 1 has been followed by User ID 2...// you will normally do thisInteraction::hasFollowed(causer:User::find(2), subject:User::find(1));// but you can also do thisInteraction::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:
useOpenSoutheners\LaravelUserInteractions\Support\Facades\Interaction;useOpenSoutheners\LaravelUserInteractions\UserInteractionType;// Query all followers of a userInteraction::to(User::find(1))->doing(UserInteractionType::Follow)->get();// Remove all followers of a userInteraction::to(User::find(1))->doing(UserInteractionType::Follow)->delete();// Query all follows of a userInteraction::from(User::find(1))->doing(UserInteractionType::Follow)->get();// Remove all follows of a userInteraction::from(User::find(1))->doing(UserInteractionType::Follow)->delete();