PHP code example of nerdymind / laravel-acquaintances

1. Go to this page and download the library: Download nerdymind/laravel-acquaintances 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/ */

    

nerdymind / laravel-acquaintances example snippets


$user1 = User::find(1);
$user2 = User::find(2);

$user1->befriend($user2);
$user2->acceptFriendRequest($user1);

// The messy breakup :(
$user2->unfriend($user1);


'providers' => [
    ...
    Multicaret\Acquaintances\AcquaintancesServiceProvider::class,
    ...
];

use Multicaret\Acquaintances\Traits\CanBeFollowed;
use Multicaret\Acquaintances\Traits\CanFollow;
use Multicaret\Acquaintances\Traits\CanLike;
use Multicaret\Acquaintances\Traits\Friendable;
//...

class User extends Model
{
    use Friendable;
    use CanLike;
    use CanFollow, CanBeFollowed;
    use CanRate, CanBeRated;
    //...
}

use Multicaret\Acquaintances\Traits\Friendable;

class User extends Model
{
    use Friendable;
}

$user->befriend($recipient);

$user->acceptFriendRequest($sender);

$user->denyFriendRequest($sender);

$user->unfriend($friend);

$user->blockFriend($friend);

$user->unblockFriend($friend);

$user->isFriendWith($friend);

$user->hasFriendRequestFrom($sender);

$user->hasSentFriendRequestTo($recipient);

$user->hasBlocked($friend);

$user->isBlockedBy($friend);

$user->getFriendship($friend);

$user->getAllFriendships();

$user->getPendingFriendships();

$user->getAcceptedFriendships();

$user->getDeniedFriendships();

$user->getBlockedFriendships();

$user->getFriendRequests();

$user->getFriendsCount();

$user->getPendingsCount();

$user->getMutualFriendsCount($otherUser);

$user->getFriends();
// or panigated
$user->getFriends($perPage = 20, $group_name);
// or paginated with certain fields 
$user->getFriends($perPage = 20, $group_name, $fields = ['id','name']);

$user->getFriendsOfFriends();
// or
$user->getFriendsOfFriends($perPage = 20);
// or 
$user->getFriendsOfFriends($perPage = 20, $fields = ['id','name']);

$user->getMutualFriends($otherUser);
// or 
$user->getMutualFriends($otherUser, $perPage = 20);
// or 
$user->getMutualFriends($otherUser, $perPage = 20, $fields = ['id','name']);

// config/acquaintances.php
...
'groups' => [
    'acquaintances' => 0,
    'close_friends' => 1,
    'family' => 2
]

$user->groupFriend($friend, $group_name);

$user->ungroupFriend($friend, 'family');

$user->ungroupFriend($friend);

$user->getFriendsCount($group_name);

$user->getAllFriendships($group_name);
$user->getAcceptedFriendships($group_name);
$user->getPendingFriendships($group_name);
...

use Multicaret\Acquaintances\Traits\CanFollow;
use Multicaret\Acquaintances\Traits\CanLike;
use Multicaret\Acquaintances\Traits\CanFavorite;
use Multicaret\Acquaintances\Traits\CanSubscribe;
use Multicaret\Acquaintances\Traits\CanVote;

class User extends Model
{
    use CanFollow, CanLike, CanFavorite, CanSubscribe, CanVote;
}

use Multicaret\Acquaintances\Traits\CanBeLiked;
use Multicaret\Acquaintances\Traits\CanBeFavorited;
use Multicaret\Acquaintances\Traits\CanBeVoted;
use Multicaret\Acquaintances\Traits\CanBeRated;

class Post extends Model
{
    use CanBeLiked, CanBeFavorited, CanBeVoted, CanBeRated;
}

$user->follow($targets)
$user->unfollow($targets)
$user->toggleFollow($targets)
$user->followings()->get() // App\User:class
$user->followings(App\Post::class)->get()
$user->isFollowing($target)

$object->followers()->get()
$object->isFollowedBy($user)
$object->followersCount() // or as attribute $object->followers_count
$object->followersCountReadable() // return readable number with precision, i.e: 5.2K

$user->rate($targets)
$user->unrate($targets)
$user->toggleRate($targets)
$user->ratings()->get() // App\User:class
$user->rateings(App\Post::class)->get()
$user->hasRated($target)

$object->raters()->get()
$object->isRatedBy($user)

$object->averageRating() // or as attribute $object->average_rating
$object->averageRatingAllTypes() // or as attribute $object->average_rating_all_types

$object->sumRating() // or as attribute $object->sum_rating
$object->sumRatingAllTypes() // or as attribute $object->sum_rating_all_types_all_types

$object->sumRatingReadable() // return readable number with precision, i.e: 5.2K
$object->sumRatingAllTypesReadable() // return readable number with precision, i.e: 5.2K


$object->ratingPercent($max = 5) // calculating the percentage based on the passed coefficient
$object->ratingPercentAllTypes($max = 5) // calculating the percentage based on the passed coefficient

// User Related: 

$object->userAverageRatingAllTypes() // or as attribute $object->user_average_rating_all_types

$object->userSumRatingAllTypes() // or as attribute $object->user_sum_rating_all_types

$object->userSumRatingReadable() // return readable number with precision, i.e: 5.2K
$object->userSumRatingAllTypesReadable() // return readable number with precision, i.e: 5.2K



$user->like($targets)
$user->unlike($targets)
$user->toggleLike($targets)
$user->hasLiked($target)
$user->likes()->get() // default object: App\User:class
$user->likes(App\Post::class)->get()

$object->likers()->get() //
$object->fans()->get() // or $object->fans. it's an alias of likers()
$object->isLikedBy($user)
$object->likersCount() // or as attribute $object->likers_count
$object->likersCountReadable() // return readable number with precision, i.e: 5.2K

$user->favorite($targets)
$user->unfavorite($targets)
$user->toggleFavorite($targets)
$user->hasFavorited($target)
$user->favorites()->get() // App\User:class
$user->favorites(App\Post::class)->get()

$object->favoriters()->get() // or $object->favoriters 
$object->isFavoritedBy($user)
$object->favoritersCount() // or as attribute $object->favoriters_count
$object->favoritersCountReadable() // return readable number with precision, i.e: 5.2K

$user->subscribe($targets)
$user->unsubscribe($targets)
$user->toggleSubscribe($targets)
$user->hasSubscribed($target)
$user->subscriptions()->get() // default object: App\User:class
$user->subscriptions(App\Post::class)->get()

$object->subscribers() // or $object->subscribers 
$object->isSubscribedBy($user)
$object->subscribersCount() // or as attribute $object->subscribers_count
$object->subscribersCountReadable() // return readable number with precision, i.e: 5.2K

$user->vote($target) // Vote with 'upvote' for default
$user->upvote($target)
$user->downvote($target)
$user->cancelVote($target)
$user->hasUpvoted($target)
$user->hasDownvoted($target)
$user->votes(App\Post::class)->get()
$user->upvotes(App\Post::class)->get()
$user->downvotes(App\Post::class)->get()

$object->voters()->get()
$object->isVotedBy($user)
$object->votersCount() // or as attribute $object->voters_count
$object->votersCountReadable() // return readable number with precision, i.e: 5.2K

$object->upvoters()->get()
$object->isUpvotedBy($user)
$object->upvotersCount() // or as attribute $object->upvoters_count
$object->upvotersCountReadable() // return readable number with precision, i.e: 5.2K

$object->downvoters()->get()
$object->isDownvotedBy($user)
$object->downvotersCount() // or as attribute $object->downvoters_count
$object->downvotersCountReadable() // return readable number with precision, i.e: 5.2K

follow(array|int|\Illuminate\Database\Eloquent\Model $targets, $class = __CLASS__)

// id / int|array
$user->follow(1); // targets: 1, $class = App\User
$user->follow(1, App\Post::class); // targets: 1, $class = App\Post
$user->follow([1, 2, 3]); // targets: [1, 2, 3], $class = App\User

// Model
$post = App\Post::find(7);
$user->follow($post); // targets: $post->id, $class = App\Post

// Model array
$posts = App\Post::popular()->get();
$user->follow($posts); // targets: [1, 2, ...], $class = App\Post

$followers = $user->followers
$followers = $user->followers()->where('id', '>', 10)->get()
$followers = $user->followers()->orderByDesc('id')->get()

use Multicaret\Acquaintances\Models\InteractionRelation;

// Get most popular object
// 1- All types
$relations = InteractionRelation::popular()->get();

// 2- subject_type = App\Post
$relations = InteractionRelation::popular(App\Post::class)->get(); 

// 3- subject_type = App\User
$relations = InteractionRelation::popular('user')->get();
 
// 4- subject_type = App\Post
$relations = InteractionRelation::popular('post')->get();

// 5- Pagination
$relations = InteractionRelation::popular(App\Post::class)->paginate(15); 


config\acquaintances.php
sh
$ php artisan migrate